- Home /
Why is Camera turning different than Player with same code in different scripts?
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. (horizontalSpeed is a constant) 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.
Answer by ray2yar · Dec 27, 2018 at 12:53 AM
If this is a third person situation you could probably attach an empty gameobject child to your character and position it to where you want the camera to be. Then you could have the camera lookat the character (or another child gameobject). However from your description I think you have a situation where you want the camera to look independently of where the character moves... so this won't get you there, but maybe in the right direction.
public Transform TargetPos;
public Transform TargetLook;
public float Speed;
void Update () {
//directly sets
transform.position = TargetPos.position;
transform.LookAt(TargetLook.position);
//the lerping version for camera follow, simplified version
transform.position = Vector3.Lerp(transform.position, TargetPos.position, 0.1f);
transform.LookAt(TargetLook.position);
}