Search Unity

2D NavMeshAgent will not move straight up or down and other strange behavior

Discussion in 'Navigation' started by Paily, Apr 19, 2022.

  1. Paily

    Paily

    Joined:
    Apr 9, 2022
    Posts:
    1
    Hi all,

    I'm trying to implement 2D click-to-move player movement with pathfinding from NavMeshPlus. At first, it seemed to work fairly well out of the box, but I'm now noticing that if I try to move directly up or down (along the Y axis, where X axis stays exactly the same), my character doesn't budge.

    For context, the reason I'm running into this with click-based movement (which normally wouldn't produce an X diff of exactly 0), is that I'm converting the click position to the center of a grid cell to implement a sort of grid-based movement (might be a dumb idea, but that's what I was trying out at least.

    Specifically, this seems to be true within a range of about 3.5 units along the Y axis. I say that because if I click further up or down the Y axis, the character seems to begin to move directly up or down and then stops about 3.5 units from wherever I clicked.

    In writing this, I noticed something even stranger that makes me think I'm really doing something wrong - the thing about moving within 3.5 units is only true if I have my character object selected in the Hierarchy while playtesting - if I have another object selected, the character doesn't seem to move at all, or budges slightly (again, only if I'm moving directly along the Y axis, any other movement is fine), and then will move within the 3.5 units once I re-select my character object in the hierarchy. In fact, if I try to move along the Y axis and randomly click between objects in my hierarchy the character seems to budge slightly each time. I have no idea why clicking different objects in my hierarchy would have any effect on the play test itself - I'm brand new to Unity but I would have thought such a thing wouldn't even be possible.

    My stripped down player controller code (I commented pretty much everything else out):

    Code (CSharp):
    1.  
    2. void Start()
    3.     {
    4.         rigidbody2d = GetComponent<Rigidbody2D>();
    5.         animator = GetComponent<Animator>();
    6.         audioSource = GetComponent<AudioSource>();
    7.         agent = GetComponent<NavMeshAgent>();
    8.  
    9.         agent.updateRotation = false;
    10.         agent.updateUpAxis = false;
    11.     }
    12.  
    13. void Update()
    14.     {
    15.         if(Input.GetMouseButtonDown(0))
    16.         {
    17.             // Gets coordinates of click, then coordinates of clicked cell, then center of clicked cell
    18.             targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); // World coordinates of click
    19.             Vector3Int cellPosition = grid.WorldToCell(targetPosition); // Cell coordinates of click ((4, 1) etc.)
    20.             targetPositionGrid = grid.GetCellCenterWorld(cellPosition); // Coordinates of center of cell ((4.5, 1.5) if cellPosition is (4, 1))
    21.             agent.SetDestination(targetPositionGrid);
    22.     }
    23.  
     
    Last edited: Apr 19, 2022
  2. Totalschaden

    Totalschaden

    Joined:
    Dec 14, 2013
    Posts:
    44
    Just ran into exactly this, did you manage to fix it ?