How do I get my objects to reach the end of the path?

Noob question here:

I’ve several gameobjects that need to reach the end of the path in the AstarAI script. I used a public Transform Target to specify where they should go and just use target.position to tell them where to head (seeker.StartPath (transform.position,target.position, OnPathComplete);), but they always stop a significant distance away from the end position.

Is there an easy way to change that or get them to simply stop at the end position?

Thanks for your help!

1 Like

@CZiporyn:

Hey, just looking around the forums and saw your post (a question I might actually be able to help on) so here goes:

So it looks like you have 2 issues: units not stopping where you expect them to, and how to get them to stop exactly at a location.

Since I don’t know any meaningful factors about how your agents move (a robot moves differently from a person and from a boat, and I’ve never used the AI scripts that come with the package) or how you are actually moving them (perhaps you are using the default example which I think uses vectors to point/move agents along the waypoints?), I’ll offer some general advice:

x) units not stopping where you expect them to: remember, the waypoints are just a collection of Vector3 points, you have to determine when your agent is “close enough” or “has reached the point” before it moves on or stops. You might do this with some sort of Vector3.Distance() check to see if your agent is within a minimum distance to that point. Keep in mind that asking if agent.postion == waypoint is asking for trouble due to float point math imprecision, so that is why a minimum distance is used instead.

x) if you want your agent to stop exactly at the waypoint, you could (when you determine a agent is close enough) just set the agent position to the waypoint (or maybe even use a lerp or moveTowards behavior). Or if you want more precise movement, just loop through the waypoints one by one, setting the agents position to the current waypoint (you could do a basic coroutine that loops every second or so and each time it does, increment the current waypoint and set the agent’s position to the new waypoint, as a basic example).

So, in the end, -you- are the person that decides how to ‘aim’ the agent at the next waypoint and when the agent is close enough to ‘aim’ at the next one, until it gets to that last waypoint.

2 Likes

Thanks, Blitzer. I think I had to understand the code a bit more (and some of the tweaking I did) to understand some of the simple reasons why my gameobject wasn’t going exactly where I wanted it to go.

Your explanation helped a lot. Thanks!

-Chris

I thought about similar translating the unit’s position to the waypoint when it was close enough but I’m worried that would result in a weird sliding effect. Do you think there is a way around that?