I have this code in the Main Camera to make the cam follow the player and to have it always behind the player.
When the player change direction I would like that the cam could turn in a smooth way and not drastically but I don’t know how to do it…
Any help would be highly appreciated as I’m totally stuck.
[SerializeField]
Transform player;
private Vector3 offsetPosition;
// Use this for initialization
void Start () {
offsetPosition = transform.position;
}
void FixedUpdate (){
transform.position = player.TransformPoint(offsetPosition);
transform.LookAt(player);
}
Use Update, not FixedUpdate. FixedUpdate is generally for updating rigidbodies. You want your camera to be in sync with rendering.
Instead of using transform.LookAt to set the rotation immediately, you want to interpolate from the current rotation to the target rotation. It’s up to you to tweak how fast that rotation should be.
Get the target rotation using: var targetRotation = Quaternion.LookRotation(player.position - transform.position)
Then you can rotate from the current to target rotations using Quaternion.RotateTowards(transform.rotation, targetRotation, maxAngle)
Where maxAngle is the amount you want to rotate per frame.