Hey all,
…
In a game where four players should move independently around a circle, if right movement for one character and left movement for another character happen at the same time, they move in the same direction, rather than around the circle in the proper way.
…
We have WASD and the arrow keys for movement in this scenario, and they seem to be mapped properly through input manager. However, the problem still persists.
…
I feel one of our variables is affecting the movement in a way we are not understanding.
…
Any help would be greatly appreciated. Thanks!
…
NOTE: The script for this player’s movement is identical to the other player’s scripts, where the only difference is the action key. (WASD, arrow keys, J and L for right/left, and [4] and [6] for right/left)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Player controller and behavior
/// </summary>
public class Player2Script : MonoBehaviour
{
/// <summary>
/// 1 - The speed of the ship
/// </summary>
// 2 - Store the movement and the component
private Vector2 movement2;
private Rigidbody2D rigidbodyComponent;
float timeCounter = 0;
void Update()
{
// 3 - Retrieve axis information
//timeCounter += Time.deltaTime;
if (Input.GetKey("j"))
{
RotateLeft();
}
if (Input.GetKey("l"))
{
RotateRight();
}
}
void FixedUpdate()
{
// 5 - Get the component and store the reference
if (rigidbodyComponent == null) rigidbodyComponent = GetComponent<Rigidbody2D>();
// 6 - Move the game object
rigidbodyComponent.velocity = movement2;
}
void RotateLeft()
{
timeCounter += Input.GetAxis("Horizontal") * Time.deltaTime * 4; // multiply all this with some speed variable (* speed);
float x = Mathf.Cos(timeCounter) * 4;
float y = Mathf.Sin(timeCounter) * 4;
float z = 0;
transform.position = new Vector3(x, y, z);
transform.Rotate(Vector3.forward * -4);
}
void RotateRight()
{
timeCounter += Input.GetAxis("Horizontal") * Time.deltaTime * 4; // multiply all this with some speed variable (* speed);
float x = Mathf.Cos(timeCounter) * 4;
float y = Mathf.Sin(timeCounter) * 4;
float z = 0;
transform.position = new Vector3(x, y, z);
transform.Rotate(Vector3.forward * 4);
}
}