- Home /
unitysteer and path
Hello,
i'm trying to implement unitysteer with path combined! first i have build opon the path demo to figure Path out. i got it working and my object is following a path.
Now i want it to combine with unitysteer. i proberly need to use AngryAntPathway to constuct my pathway from an arraylist of path and such. But i could not get it to work. perhaps any1 has an example?
Answer by Ricardo · Nov 14, 2009 at 02:20 PM
Here's some sample code, snipped from a larger project. You'll need a MonoBehavior that is an ISearchMonitor (for Path).
public class Ship : MonoBehaviour, ISearchMonitor, IRadarReceiver {
In this case we have three properties; the first is the list of nodes returned by Path, the second a path seeker, and finally a UnitySteer PathFollower vehicle.
protected ArrayList path;
protected Seeker seeker;
private PathFollower shipVehicle;
On the behavior start we initialize the PathFollower vehicle to the current object's transform. Notice that this vehicle did not move vertically, but that was specific my implementation case.
protected void Start () {
shipVehicle = new PathFollower(this.transform, 1, null, radius);
shipVehicle.MovesVertically = false;
shipVehicle.MinCollisionTime = 2;
shipVehicle.MaxSpeed = maxSpeed;
shipVehicle.Mass = 0.1f;
shipVehicle.MaxForce = maxForce;
shipVehicle.AvoidDeg = 30;
}
The GoTo method is pretty much Path boilerplate - it creates a seeker, adds a monitor and starts it on the control. I'm just adding it here for reference purposes.
public void GoTo(Vector3 destination)
{
seeker = new Seeker( transform.position, destination, maxFrameTime, radius, null );
if( seeker.Start != null && seeker.End != null )
{
this.Moving = true;
seeker.AddMonitor(this);
Control.Instance.StartSeeker( seeker );
}
}
When we receive a notification that the search was completed, we create a UnitySteer AngryAntPathway based on the resulting path ArrayList, and pass it to the vehicle for following.
public void OnSearchCompleted( Seeker seeker ) { this.seeker = null; this.path = seeker.Solution; this.destination = seeker.To;
if (this.path == null)
{
return;
}
if (seeker.Start != seeker.End)
{
shipVehicle.Pathway = new AngryAntPathway(path, 0.25f, false);
}
else
{
shipVehicle.Moving = false;
}
}
And finally, if we have a path, on Update we tell the vehicle to update our position.
protected void Update () {
if (path == null) return;
shipVehicle.Update(Time.deltaTime);
}
That's it. :-)
Hi there, i got it almost working i think.
But for some reason my object goes straight to the final destination, just seems to ignore the path.. any clue what that could be ? I got my code just as yours but with some things fron the path demo.
The code looks O$$anonymous$$ at a glance. Check out the path being returned - does it actually contain more than one stop? What do the Debug.DrawLine calls from the vehicle show?
Hi. I am having a terrible time trying to figure this all out. I have path setup, and am trying to implement the unitysteer portion. Piecing together the script from above i keep getting numerous errors. the one that i cannot figure out is this: " The type or namespace name `AngryAntPathway' could not be found. Are you missing a using directive or an assembly reference?". As best as i know i am including the correct library to get this to work. any help would be greatly appreciated.
n8, you'll notice that AngryAntPathway is not compiling by default, as the UnitySteer distribution does not include Path. You should remove the "#if false" at the top and "#endif" at the bottom of the class file.
Awesome! Thanks, that got everything mostly working. One more quick question. I have a race scenario and I need my vehicle to cycle through multiple points. Is there an event that I can listen for to know when my vehicle has reached the destination in order to switch to the next destination? thanks in advance for any help you can offer?