1
\$\begingroup\$

enter image description here

Imagine the blue part of this picture is streets and navmesh.
the red object is a car with navmeshAgent.
the green circles are the colliders set as triggered and they are the destination of the car.

when the game starts the car will choose one of them and go to that , after reach that it will choose another one randomly so it means the car will go through all of this forever.

now the problem is sometimes when car reach's one them and choose another since it will choose the closer path to next destination sometimes it will have to turn back which its not realistic and doesn't make sense.

enter image description here

as you can see in this example when reach that point since the middle one is closer from where the car is triggered it will turn back and the red arrow is wrong path , instead it should go through the green arrow and that's the correct path.
how can i do something to not to turn back instead find another way?

\$\endgroup\$
1
\$\begingroup\$

I think the most obvious solution is to forbid the path you have just taken. This requires two checks. We keep track of where the car has been, and find the next best way it should go.

In the below image I've added four purple squares which exist between the randomly selected destinations. The car object has a variable, let's call it lastPath. Whenever the car enters one of the squares, it assigns that square to lastPath, so it always knows where it has been.

When it arrives at destination it then finds two things. It randomly finds the next destination, and then also finds the closest purple square from its current position to the destination which is NOT lastPath! We shall call this nextPath.

All you do then is pathfind twice, from car to nextPath, and then from nextPath to destination. That way it should avoid the path it has just taken.

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.