using UnityEngine;
public class CameraFollow : MonoBehaviour {
public Transform target;
public float smoothingspeed_movement = 0.125f;
public float smoothingspeed_rotation= 0.5f;
public Vector3 offset;
void FixedUpdate ()
{
Vector3 desiredPosition = target.position + offset;
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothingspeed_movement);
transform.position = smoothedPosition;
Vector3 desiredRotation = target.eulerAngles + offset;
Vector3 smoothedRotation = Vector3.Lerp(transform.rotation.eulerAngles, desiredRotation, smoothingspeed_rotation);
transform.eulerAngles = smoothedRotation;
}
}
The problem I am facing is that when the camera moved to the left, from rotation 0, back to 359, it smooths the movement towards the right, instead of smoothing to the left. So it always does a 360-rotation.
New contributor
Lulu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Instead of interpolating the euler angles, you can spherically interpolate (slerp) on two quaternions: the old rotation, and the new rotation:
Quaternion desiredRotation = target.rotation * Quaternion.Euler(offset);
Quaternion smoothedRotation = Quaternion.Slerp(transform.rotation, desiredRotation, smoothingspeed_rotation);
transform.rotation = smoothedRotation;
1