- Home /
Problems with follow script (pet like)
Hi people.
I'm trying to make a simple script to make an object follow another object smoothly. I've seen some examples here in other answers, but in all of them I'm having the same problem (including my solution which is below). My problem is, the object do follow the target object though when it reaches the minimum distance it starts to move very very slowly to a random direction or in a circle trajectory around the target object. Can anyone tell me what am I doing wrong?
using UnityEngine;
using System.Collections;
public class AllyFollow : MonoBehaviour {
public Transform target;
public float smoothTime = 0.3f;
public float distance = 2.5f;
public float correction = 0.1f;
private Vector2 velocity;
private Vector3 thisPosition;
private Transform thisTransform;
void Start ()
{
thisTransform = transform;
}
void Update ()
{
// Look at target
Vector3 targetRelativeDirection = target.position - thisTransform.position;
targetRelativeDirection.y = 0;
thisTransform.forward = targetRelativeDirection;
// Seems to work fine up to here...
Debug.Log("value 1 - " + targetRelativeDirection.magnitude);
Debug.Log("value 2 - " + (distance + correction));
// Follow target if distance between me and target is
if (targetRelativeDirection.magnitude > distance + correction)
{
Debug.Log("FOLLOWING");
thisPosition = thisTransform.position;
thisPosition.x = Mathf.SmoothDamp(thisTransform.position.x, (target.position.x - (distance * targetRelativeDirection.normalized.x)), ref velocity.x, smoothTime);
thisPosition.z = Mathf.SmoothDamp(thisTransform.position.z, (target.position.z - (distance * targetRelativeDirection.normalized.z)), ref velocity.y, smoothTime);
thisTransform.position = thisPosition;
}
else
{
Debug.Log("NOT FOLLOWING");
}
}
}
I figured it out. I shouldn't be moving this object by changing its transform properties because it won't respect any colliders or physics calculations. This movement problem I was having was due to the physics being applied to an object that was having it's transform component changed directly. This was conflicting with the physics calculations.
What I recommend for people trying to do this or other similar things is to never move an object changing it's transform properties unless you want to warp it into a place. If you want movement to feel natural like a character following another character, add a character controller or some other physics component to the object and move them using the methods of these components, like CharacterController.Simple$$anonymous$$ove.
Thanks anyway!
Answer by MouseCaneta · Mar 20, 2012 at 11:12 PM
I figured it out. I shouldn't be moving this object by changing its transform properties because it won't respect any colliders or physics calculations. This movement problem I was having was due to the physics being applied to an object that was having it's transform component changed directly. This was conflicting with the physics calculations.
What I recommend for people trying to do this or other similar things is to never move an object changing it's transform properties unless you want to warp it into a place. If you want movement to feel natural like a character following another character, add a character controller or some other physics component to the object and move them using the methods of these components, like CharacterController.SimpleMove.
Thanks anyway!
Answer by DaveA · Mar 20, 2012 at 10:23 PM
I've seen this before. What I do is check if the distance is small (like .01) and set a boolean 'moving' to false. So as soon as it gets in that radius, it stops. And you need to check against a somewhat larger distance to start again, like .015 or something. Hysteresis I think it's called.
Your answer
![](https://koobas.hobune.stream/wayback/20220613065345im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Smooth Camera Follow Script, Weird Movement... Please help! 1 Answer
Unity 2D Object Smooth Follow Problem C# 2 Answers
Timer activates script 1 Answer
smooth follow only on X and Y 1 Answer