# Make Object smoothly follow player if out of near range

I just wrote a script that lets an object follow the player at all times if the player is a certain range away from the object (to be used as a camera target object).
It basically works just the way I want it to work but the movement is a bit jittery and I have no idea why.
I tested a bunch of different ways to do it and even though this one works I’m sure there is a much better way to do it… I’m fairly new to unity so I wouldn’t actually know -.-

Anyways, the script moves the gameobject towards the target(player) when the player is farther than 0.75 Units aways … so effectively the camera only starts to move when the player moves at least that amount in any direction.

Heres what I’ve got so far:

``````using UnityEngine;
using System.Collections;

public class CameraTargetFollowPlayer : MonoBehaviour {

public float targetDistance = 0.5F;
public float smoothTime = 10;
public Transform target;

private float distance = 1F;

Transform temptarget = null;

void LateUpdate () {

transform.LookAt(target);

distance = Vector3.Distance( transform.position, target.position) ;

// reset if its too far away
if (distance > 7) {
transform.position =  target.position-transform.forward;
} else {

if (distance > targetDistance) {

transform.position = Vector3.Slerp( transform.position, new Vector3 (
target.position.x-(transform.forward.x*targetDistance),
target.position.y,
target.position.z-(transform.forward.z*targetDistance)
),
Time.deltaTime * smoothTime
);

} else {
// if distance isnt big enough to move the target we still need to adjust the y height every frame
transform.position = new Vector3(transform.position.x, target.position.y, transform.position.z);

}

}

}

}
``````

Alright … it kind of solved itself I think. The problem isn’t within this follow script but my camera movement code …

So yeah … if anyone ever needs some script for smoothly following something at a min distance, here it is