0

My Camera Follow script is not very smooth. How can I smoothen the movement of the camera?

Here it is:

using UnityEngine;
using System.Collections;

public class FollowCamera : MonoBehaviour {

    public float interpVelocity;
    public float minDistance;
    public float followDistance;
    public GameObject target;
    public Vector3 offset;
    Vector3 targetPos;

    void Start () {
        targetPos = transform.position;
    }

    void FixedUpdate () {
        if (target)
        {
            Vector3 posNoZ = transform.position;
            posNoZ.z = target.transform.position.z;

            Vector3 targetDirection = (target.transform.position - posNoZ);

            interpVelocity = targetDirection.magnitude * 5f;

            targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime); 

            transform.position = Vector3.Lerp( transform.position, targetPos + offset, 0.25f);

        }
    }
}

The script makes the camera follow a rotating target.

animuson
  • 53,861
  • 28
  • 137
  • 147
  • Move it from FixedUpdate to LateUpdate. FixedUpdate usually is called less often then Update and Camera movement is best on LateUpdate. – Jerry Switalski Jan 31 '16 at 10:38
  • Also read the thousands of exact duplicates of this question http://stackoverflow.com/questions/26640891/unity3d-choppy-camera-motion – Fattie Jan 31 '16 at 14:19

1 Answers1

2

You are updatng camera position on FixedUpdate. Change it to LateUpdate. FixedUpdate is designed for other purposes and is called less often usually then every frame. LateUpdate is called every frame and after Update so if your target is updated on Update camera will update its position later, what is desired.

Jerry Switalski
  • 2,690
  • 1
  • 18
  • 36