using UnityEngine;
using System.Collections;
public class KeepDistance : MonoBehaviour {
// Use this for initialization
void Start () {
}
//Restricts the position of this object to be within a distance of "distance" of "center"
public float distance = 40;
public Transform center;
// Update is called once per frame
void Update () {
float dst = Vector3.Distance(center.position, transform.position);
if (dst > distance)
{
Vector3 vect = center.position - transform.position;
vect = vect.normalized;
vect *= (dst-distance);
transform.position += vect;
}
}
}
I need to keep the object trailing behind the player. When I have a positive number for the variable distance the object stays in front of me at the same distance. When I have a negative number for the variable distance the object blinks in front and in back of the player.