- Home /
An object to follow another object through the waypoint - problem: taking a shortest path instead of walking through the waypoint.
Hello, I have created an object which is moving good along the waypoints. This is the first part. And I have introduced another object. And have calculated the distance between the second object and all other waypoints.
My objective is to move the first object to the waypoints, which are near to the second object. Which is to be done dynamically. And I have taken the waypoints in an array, initially. My code goes like this:-
var waypoint : Transform[];
var dist1 = Vector3.Distance(waypoint[1].position,object2.transform.position);
var dist2 = Vector3.Distance(waypoint[2].position,object2.transform.position);
var dist3 = Vector3.Distance(waypoint[3].position,object2.transform.position);
var dist4 = Vector3.Distance(waypoint[4].position,object2.transform.position);
var dist5 = Vector3.Distance(waypoint[5].position,object2.transform.position);
var dist6 = Vector3.Distance(waypoint[6].position,object2.transform.position);
var minimumDistance = Mathf.Min(dist1,dist2,dist3,dist4,dist5,dist6);
if(minimumDistance==dist1)
{
object1.transform.position = waypoint[1].position;
object1.animation.Play("walk");
}
if(minimumDistance==dist2)
{
object1.transform.position = waypoint[2].position;
object1.animation.Play("walk");
}
if(minimumDistance==dist3)
{
girl.transform.position = waypoint[3].position;
object1.animation.Play("walk");
}
if(minimumDistance==dist4)
{
object1.transform.position = waypoint[4].position;
object1.animation.Play("walk");
}
if(minimumDistance==dist5)
{
object1.transform.position = waypoint[5].position;
object1.animation.Play("walk");
}
if(minimumDistance==dist6)
{
object1.transform.position = waypoint[6].position;
object1.animation.Play("walk");
}
I am sorry. This is trivial, I believe. But I am not able to do animation. But I have mentioned it in the script. But why it isn't happening, I don't know. Help! Thanks in advance.
Does this happen in Update? Because that would mean you are instantly teleporting to positions, and restarting the animation every frame.
I apologize. I did not mention. Yes, it happens in Update().
Then you have a problem. You should only be calling this code exactly when you want your object to move- what happens here is the animation gets restarted every frame, and never gets a chance to play! I've posted a cleaner version of your current code below, but it will still have the same problem if you use it in the same way.
Could you explain what you are trying to do here a little better? I'm still not quite sure what the point of this code is, since it looks like it teleports 'object1', meaning a walk animation isn't appropriate anyway!
Alright. See, I am building a car showroom, where the object2(customer)(3rd person controller) arrives at the car. I have given waypoints around the car, so that the object1(salesgirl) moves with the object2, but through the waypoints only.
Here I am calculating the $$anonymous$$imum distance between object2 and the waypoints. The object1(salesgirl) should walk to the nearest waypoint to object2(customer). This is my objective.
Even though I have given,
if(condition) { velocity = moveDirection.normalized * speed; girl.animation.Play("walk"); }
else { velocity = Vector3.zero; girl.animation.Play("idle"); }
It is not walking! And, yes, I have given this in Update().
Answer by syclamoth · Dec 01, 2011 at 11:12 AM
Well, I have no idea how to fix your actual problem, since you haven't given me enough details, but that code needs to be a lot cleaner.
var waypoints : Transform[];
var closestWaypoint : Transform;
var closestDistance : float = 9999999;
for(var waypoint : Transform in waypoints)
{
// Check every waypoint
var curDist : float = Vector3.Distance(waypoint.position, object2.transform.position);
if(curDist < closestDistance)
{
// This is the closet one yet!
closestDistance = curDist;
closestWaypoint = waypoint;
}
}
object1.transform.position = closestWaypoint.position;
object1.animation.Play("walk");
Much nicer! Also, you can use this with any number of waypoints without having to add extra lines.