Input AnyKey only working for one of my players?
Player One isn’t rotating properly. When moving diagonally, if the player tries to rotate at the same time, the player will sometimes stop rotating or stop all together.
I've tried attaching it to an empty cube (plus rigid-body) and am having the same issue.
The weirdest part is that the code is identical for my second player, the prefab is also identical, and yet I'm not having any issues with their rotation. The only difference is that the second players rotation uses 'K' and 'L' keys instead of player one's 'G' and 'H' keys.
Here's my movement code. Horizontal2 and Vertical2 are simply copies of the Horizontal and Vertical axis, but with the keys changed to only take in left, right, up, down (and Horizontal/Vertical has had those keys removed).
void Update()
{
if (gameObject.CompareTag("Player"))
{
if (Input.GetKey(KeyCode.H))
{
Quaternion rot = Quaternion.Euler(Vector3.up * rotationSpeed);
playerRb.MoveRotation(playerRb.rotation * rot);
}
if (Input.GetKey(KeyCode.G))
{
Quaternion rot = Quaternion.Euler(Vector3.up * -rotationSpeed);
playerRb.MoveRotation(playerRb.rotation * rot);
}
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
playerRb.AddForce(Vector3.forward * speed * verticalInput);
playerRb.AddForce(Vector3.right * speed * horizontalInput);
}
if (gameObject.CompareTag("Player2"))
{
if (Input.GetKey(KeyCode.L))
{
Quaternion rot = Quaternion.Euler(Vector3.up * rotationSpeed);
playerRb.MoveRotation(playerRb.rotation * rot);
}
if (Input.GetKey(KeyCode.K))
{
Quaternion rot = Quaternion.Euler(Vector3.up * -rotationSpeed);
playerRb.MoveRotation(playerRb.rotation * rot);
}
float horizontalInput2 = Input.GetAxis("Horizontal2");
float verticalInput2 = Input.GetAxis("Vertical2");
playerRb.AddForce(Vector3.forward * speed * verticalInput2);
playerRb.AddForce(Vector3.right * speed * horizontalInput2);
}
}
Your answer
Follow this Question
Related Questions
Acceleration only increasing when I press a key 0 Answers
Why does the player not move around planet with gravity ? 1 Answer
i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers
i am using a code to make my cube walk with character controller ,but when i play the cube spins. 0 Answers
Can't find a way to make object rotate and move at the same time. 1 Answer