I’m trying to get an object to follow my player but stop within a given distance. The following is my code, but the object doesn’t appear to be following my player, but I’m not sure why.
public Transform target;
public float maxDistance;
public float distance;
PlayerController playerController;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
distance = Vector2.Distance(transform.position, target.position);
if (maxDistance <= distance)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, Time.deltaTime * playerController.moveSpeed);
}
}
}