2
votes

I am having a problem with the camera following the player. When the player moves, the camera shakes and this effect is more noticeable when the player is in the crouched position. I am using root motion for the player with animations by mixamo.

Player Script:

Transform cameraT;
void Start () {
cameraT = Camera.main.transform;
}
void FixedUpdate ()
{
float sideMotion = Input.GetAxis("SideMotion");
float straightMotion= Input.GetAxis("StraightMotion");
if (Mathf.Abs(sideMotion) > 0 || Mathf.Abs(straightMotion) > 0)
     {
         transform.eulerAngles = new Vector3(transform.eulerAngles.x, 
cameraT.eulerAngles.y);

     }
}

Camera Script:

public float distanceFromPlayer=2f;

 public float mouseSensitivity=6;

 public Transform player;

 public Vector2 pitchConstraint= new Vector2(-30,80);

 Vector3 rotationSmoothVelocity;
 Vector3 currentRotation;
 public float rotationSmoothTime = 0.2f;

 float yaw; //Rotation around the vertical axis is called yaw

 float pitch; //Rotation around the side-to-side axis is called pitch

 private void LateUpdate()
 {
     yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
     pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
     pitch = Mathf.Clamp(pitch, pitchConstraint.x, pitchConstraint.y);

     currentRotation = Vector3.SmoothDamp
         (currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);


     transform.eulerAngles = currentRotation;

     transform.position = player.position - transform.forward * distanceFromPlayer;
 }

For public transform I just added an empty game object to player chest level and parented it to the player. I got the rotating camera smoothly trick from an online tutorial but that exact thing won't work on player rotation though.

A character controller is attached to player without any rigidbody or collider.

1

1 Answers

0
votes

You are going to want to use Lerp on the camera in order to smooth its motion from one position to another. The Scripting API has a good example of Vector3.Lerp.

So in your case, you could add a public variable for smoothing position as well. Something like positionSmoothTime. Then make a "Desired Position" variable, we'll call it destPosition.

Vector3 destPosition = player.position - transform.forward * distanceFromPlayer;

transform.position = Vector3.Lerp(transform.position, destPosition, positionSmoothTime);

This should effectively smooth it to where the stuttering should go away. Another thing that will help that someone else mentioned is using a part of the body that moves less (Like the head) as the target. This combined with the Lerp function will give you a smooth camera movement.

Another large help would be to move things into the Update() function. The reason being is FixedUpdate() doesn't get called every frame. So you could potentially get stutter as a result. If you move everything into Update() it will update every frame and help smooth things out. If you do this though you will need to multiply all movement by Time.deltaTime as shown in the example below.

Vector3 destPosition = (player.position - transform.forward * distanceFromPlayer) * Time.deltaTime;

For more examples, check the links to each function to see Unity's documentation on it. It has examples of everything I've shown here.