I'm making a 2D game in Unity. For 2D I am using the NavmeshSurface2D-plus master package available on github. My enemy object is chasing my main character nicely, it's ok. I set my main character to only move in four directions (with Vector2.left, right, up, down). Likewise, how do I move my enemy object with the "Nav Mesh Agent" component?

If I didn't use "NavMeshAgent", I could only make my enemy object move up, down, left, right under certain conditions. But at that time, since I wasn't using NavMesh, my enemy object would be stuck on the walls.

EDIT: As an example, the image below shows the path followed by the turquoise enemy character to the yellow main character. (agent.SetDestination(target.position);) I examined the corners of this path by assigning them to a Vector3 list with "agent.path.corners", but I guess it is not possible to make any changes on them. https://i.sstatic.net/fSWvv.jpg

The image below is the route I want it to be. (yellow line) I can't do this using "NavMeshagent", I thought to learn path and corner coordinates and set them manually, but this is not a general solution. https://i.sstatic.net/cn3zl.jpg

Corner information of the path followed by the navmesh agent as below:

List<Vector3> corner= new List<Vector3>();

public void PathMove(NavMeshPath path)
    Vector3 previousCorner = path.corners[0];
    int i = 1;
    while (i < path.corners.Length)
        Vector3 currentCorner = path.corners[i];
        previousCorner = currentCorner;
    foreach (Vector3 item in corner)
  • 1
    \$\begingroup\$ You can query NavMeshAgent.path to read the current path, decide whether to move vertically or horizontally based on the next segment of that path, then move it manually, no? NavMeshAgent.isStopped will let you park the agent so it doesn't do any automatic diagonal movement of its own. \$\endgroup\$
    – DMGregory
    Commented Jan 11, 2022 at 18:27
  • \$\begingroup\$ I thought about your suggestion and edited my question. \$\endgroup\$
    – gunza
    Commented Jan 12, 2022 at 22:12


You must log in to answer this question.

Browse other questions tagged .