- Home /
Simple follow script issue
Hello,
I've been trying this script I've only slightly modified for my use. I just want my object to follow my player, and stop when in range. Now it all works fine when I don't have a Rigidbody on my following object. It stops fine but the object slowly floats and transforms to what seems to be the middle of my player character.
To counteract this I added a rigidbody, it jitters along the floor (As I imagine because it's trying to float whilst the gravity from the ridigbody is trying to pull it down) When it stops, it's very jittery.
I've tried to disable the rigidbody whilst not moving, but still has an issue.
Could someone offer any guidance on what I could try? Thanks!
var target : Transform;
var rotationSpeed = 3.0;
var moveSpeed = 3.0;
var maxAttackDistance = 0.0;
var isFree : boolean = true;
private var myPosition : Transform;
function Awake()
{
myPosition = transform;
}
function Start()
{
var go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
}
function Update()
{
if(isFree == true)
{
PlayerDetected();
}
else
{
animation.Play("Idle");
}
}
function PlayerDetected()
{
myPosition.rotation = Quaternion.Slerp(myPosition.rotation, Quaternion.LookRotation(target.position - myPosition.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myPosition.position) >= maxAttackDistance)
{
myPosition.position += transform.forward * moveSpeed * Time.deltaTime;
animation.Play("Run");
}
else
{
animation.Play("Idle");
}
}
Answer by Socrates · Mar 30, 2015 at 08:44 AM
On line 39, you are looking at the distance between the target's position and this object's position. Then you move toward the target's position as long as that distance is greater than or equal to 0.0 (see line 5). That is causing your problem.
You do not want to match the target's position exactly. You want to move to the target's position less an offset. You have to leave room for size of both the target and the object.
Think of it this way: Pretend both target and object are spheres. You want to move them next to each other. If you get to where Vector3.Distance(target,object) is less than the radius size of the target plus the radius size of the object, the two spheres have collided and are partially inside each other.
So, you need to make sure your maxAttackDistance is set to be large enough to reflect how "thick" the object and the target are. If you will want to add in some extra distance to this if you do not want the object to touch your target at all.
If the object has any issues with the vertical motion after the buffer is in place, you may want to take a look at computing the Distance only in X and Z, then adjusting the Y separately to get the object to be hovering at the correct height. (i.e. If you want the object to float at the target's head height, not at the target's center point.)
That was a good explanation, I'm curious whether there is a way to force the Y value to be frozen?
I guess as a work around I could add an empty gameobject to the height of which I want to AI to stay (Attached to my player) as the target.
If you are using a rigidbody, you can lock the position or rotation on the three axes in the Inspector.
But you're probably not going to use a rigidbody for this. I was mostly thinking of what you were saying that when you did use a rigidbody, you were getting vertical jittering along the floor.
As a simple example of moving just X and Z:
// Assume that computed$$anonymous$$ove is a Vector3 you've put together already, but which as Y move too.
Vector3 limited$$anonymous$$ovement = new Vector3(computed$$anonymous$$ove.x, 0f, computed$$anonymous$$ove.z);
myPosition.position += limited$$anonymous$$ovement * moveSpeed * Time.deltaTime;
I think that makes sense.
I modified it slightly because it looked a little like C#.
var computed$$anonymous$$ove : Vector3;
var limited$$anonymous$$ovement : Vector3 = new Vector3(computed$$anonymous$$ove.x, 0f, computed$$anonymous$$ove.z);
myPosition.position += limited$$anonymous$$ovement * moveSpeed * Time.deltaTime;
How do I make it translate forward? If I'm not using "transform.forward"? I understand that we need to use computed$$anonymous$$ove but how do we use it?
Thanks again!
Your answer
Follow this Question
Related Questions
Enemy following Player in range 2 Answers
Help with Enemy AI 1 Answer
How I can make a simple follow AI in Unity 5? 1 Answer
Enemy not rotating when inside range 1 Answer