Why is Camera turning different than Player with same code in different scripts?

60 Views Asked by At

I want to integrate a running character in my game, I downloaded the animation via Mixamo. The player is running and if I attach the camera directly to the Player it's shaking the camera and the game is not playable. So I made a different script for the camera. In there, the camera follows always the player but turns, like the player, via

    float h = PlayerMovement.horizontalSpeed * Input.GetAxis("Mouse X");

    transform.Rotate(0, h * Time.deltaTime * 60, 0);

(for the camera) and

    float h = horizontalSpeed * Input.GetAxis("Mouse X");

    transform.Rotate(0, h * Time.deltaTime * 60, 0);

for the player. But after a view seconds I'm running in a completely different way then I look. Why is that and how can I solve this?

I tried different animations - nothing worked.

Update: If there is no rigidbody and no collider attached to it it works fine. But I need them.

1

There are 1 best solutions below

4
On

remove this:

float h = horizontalSpeed * Input.GetAxis("Mouse X");

transform.Rotate(0, h * Time.deltaTime * 60, 0);

and drag the camera onto the play. as a child of the player the camera will follow and turn with the play.

if the camera is already a child of player, this would explain why your getting a messed up turn, because your changeing the players angle, by x, which changes the camera angle by x, and then your changeing the camera angle by x again, making them not line up.