0
\$\begingroup\$

I want to integrate a running character in my game, so I downloaded an 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. Therefor I made a different script for the camera. In there, the camera is always following 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. (horizontalSpeed is a constant) But after a few 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.

\$\endgroup\$
4
  • \$\begingroup\$ Is it a 3rd person camera (ie behind the player with player in view), because here you're just rotating the camera, but to follow the players view you'd need to update the cameras position and rotation so that its behind the player and facing the same way. \$\endgroup\$ – Big T Larrity Dec 26 '18 at 18:37
  • \$\begingroup\$ As I mentioned the camera is following the player always via the camera script. And it is a first person game, so the speed and center of turning has to be the same. But because the Player is shaking, the camera is as well if I would sync the Y-Rotation-Value \$\endgroup\$ – Lukas Dec 26 '18 at 18:50
  • \$\begingroup\$ I'm not sure what the problem is by looking at this. But perhaps you might try LateUpdate instead of Update. Just an idea. Sorry i couldnt help more \$\endgroup\$ – Big T Larrity Dec 26 '18 at 18:54
  • \$\begingroup\$ Also, make a parent to the Player game object, then make the camera a child of that parent (and the player also a child of it) \$\endgroup\$ – Big T Larrity Dec 26 '18 at 18:57
0
\$\begingroup\$

Try updating the player’s rotation through its rigidbody component’s MoveRotation method. Make sure to do this in FixedUpdate.

void FixedUpdate() {
  float h = horizontalSpeed * Input.GetAxis("Mouse X");
  rigidbody.MoveRotation(rigidbody.rotation * Quaternion.Euler(0, h * Time.deltaTime * 60, 0));
}

The Camera’s rotation would also need to be calculated in FixedUpdate.

\$\endgroup\$
2
  • \$\begingroup\$ Any particular reason not to use Time.fixedDeltaTime in FixedUpdate? \$\endgroup\$ – user109982 Jan 21 '20 at 6:43
  • \$\begingroup\$ No reason. Inside FixedUpdate they are identical though, so the only difference is in readability \$\endgroup\$ – Ed Marty Jan 21 '20 at 14:47
0
\$\begingroup\$

You dont need to add a movement script to the camera because if you make camera the child of player then it will follow the player without the script

\$\endgroup\$
1
  • \$\begingroup\$ As mentioned by the OP, the camera is shaking in that case, making it unplayable. (probably because it takes over all movement the player makes directly.) \$\endgroup\$ – D.Kallan Jan 6 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.