Porting the Pathfinding Unity Mecanim example (code inside)

Hi all,

It’s just a copy/paste of my post in the official Unity forum.

Here is my first try at using Mecanim with A* Pathfinding Project

step 1 : Take the “Nav Mesh Example” scene from Unity Mecanim example.

step 2 : Add a A* Pathfinding graph, in my test, I’ve added a GridGraph of 160 x 120 with a node size of 0.4 and with a center at 0;0;0.

step 3 : Create a script named AgentAStar.cs with the code linked.

step 4 : Modify the TeddyBear object from the scene like that:

step 5: run the scene and left click on the ground to move the bear.

Possible improvements:

  • Add all Crates in an obstacle layer and mask it in the graph
  • Tweak modifiers, node size, erosion …etc.
  • If you own the Pro version, you can use a recast Graph for better results.

If you have a better implementation or a different working code related to Mecanim with A* pathFinding, feel free to post it here.


I will have to try this out! :slight_smile:

Making discussion sticky

Hi Aron, this is really a first try, I have used a very simple code for following the path. Your AIPath class could probably give better result. Better graph parameters and modifiers can also improve the result.

I have tried with a RecastGraph but I’ve got some difficulties to generate a nice navmesh.

For those who don’t have Unity Pro, I’ve generated a webplayer of the official Unity demo with a screen-shot of their navmesh and agent parameters, to compare with A* implementation.

Thank you for posting this. What on earth is a “Locomotion”? It is not in the Unity namespace or the A* Pathfinding namespace.

The “Locomotion” class is part of the mecanim demo, it’s the agent.

Calling locomotion.Do(speed, angle) moves the bear in the demo.