OK, let me first start off by saying that SmoothFollow is one of the simplest things you can do, as a matter of fact in JS is is a one liner.
#pragma strict
public var target: Transform;
public var smoothness: float = 3.0;
function Start () {
}
function Update () {
if(!target){ return; }
transform.position = Vector3.Lerp(transform.position, target.position, smoothness * Time.deltaTime);
}
What you are trying to do is just a little more complex. You want to limit the distance from the character.
#pragma strict
public var target: Transform;
public var smoothness: float = 3.0;
public var maxDistance: float = 5.0;
function Start () {
}
function Update () {
if(!target){ return; }
transform.position = Vector3.Lerp(transform.position, target.position, smoothness * Time.deltaTime);
if(Vector3.Distance(transform.position, target.position) > maxDistance)
transform.position = target.position + (transform.position - target.position).normalized * maxDistance;
}
Lastly, if you want a minimum distance added, to prevent it from clipping or colliding with the object, you would simply add a few more lines of code.
#pragma strict
public var target: Transform;
public var smoothness: float = 3.0;
public var minDistance: float = 1.0;
public var maxDistance: float = 5.0;
function Start () {
}
function Update () {
if(!target){ return; }
if(Vector3.Distance(transform.position, target.position) < minDistance){
transform.position = target.position + (transform.position - target.position).normalized * minDistance;
return; // get out if we are too close, this prevents lerping it closer.
}
transform.position = Vector3.Lerp(transform.position, target.position, smoothness * Time.deltaTime);
if(Vector3.Distance(transform.position, target.position) > maxDistance)
transform.position = target.position + (transform.position - target.position).normalized * maxDistance;
}
Now, if you wanted to add a constant offset, such as on the Z plane, you will need to add something to do that. (Note, you dont need the min distance for this.)
#pragma strict
public var target: Transform;
public var smoothness: float = 3.0;
public var maxDistance: float = 5.0;
public var zOffset : float = -10.0;
function Start () {
}
function Update () {
if(!target){ return; }
transform.position = new Vector3(transform.position.x, transform.position.y, target.position.z);
transform.position = Vector3.Lerp(transform.position, target.position, smoothness * Time.deltaTime);
if(Vector3.Distance(transform.position, target.position) > maxDistance)
transform.position = target.position + (transform.position - target.position).normalized * maxDistance;
transform.position = new Vector3(transform.position.x, transform.position.y, zOffset);
}