Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Pathfinding only working on one agent at a time [2D]

Discussion in 'Navigation' started by QueenRosa, Oct 10, 2023.

  1. QueenRosa

    QueenRosa

    Joined:
    May 10, 2023
    Posts:
    16
    My agentmesh works in scene view, and in game view, but is invisible in game view? This is the code I use, and the settings for the agent.



    Code (CSharp):
    1.     Transform player;
    2.     UnityEngine.AI.NavMeshAgent agent;
    3.  
    4.     private void Start()
    5.  
    6.     {
    7.         agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
    8.         agent.updateRotation = false;
    9.         agent.updateUpAxis = false;
    10.  
    11.     }
    12.  
    13.  
    14.     public void init()
    15.     {
    16.         player = GameObject.Find("Player").transform;
    17.  
    18.     }
    19.  
    20.  
    21.  
    22.     public void goToPlayer()
    23.     {
    24.  
    25.         agent.SetDestination(player.position);
    26.  
    27.     }
    28.        
    29.  
    30. }


    And my navmesh settings:




    Now the other problem I have. I insert that script into two different gameobjects. It works on both, as in, the start function does. But when gotoplayer() is called, it only works on one at a time. I can't figure out why this is. If I remove the script from one, it'll work on the other and vice versa. But never at the same time.

    I've even tried setting different targets for both objects thinking maybe that had something to do with it, but still, it doesn't work.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    are you calling goToPlayer() on both scripts? i guess you are calling it from another script?
     
  3. QueenRosa

    QueenRosa

    Joined:
    May 10, 2023
    Posts:
    16
    Yes. Im calling gotoplayer from another script. That should call it to ALL objects that have the script. No?
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    No, it will only call the one that you are referencing at.

    One way to keep track of many agents
    would be adding them to list when they are spawned,
    then loop the list and call that method for all.
     
  5. QueenRosa

    QueenRosa

    Joined:
    May 10, 2023
    Posts:
    16
    oh ok. Well what about them disappearing at game view?
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    do they have some mesh renderer or other visible object on them?
     
  7. QueenRosa

    QueenRosa

    Joined:
    May 10, 2023
    Posts:
    16
    yes they have sprite renderer with a sprite, I can see them in scene view and they move around there too but I can’t see them in game view, but in gizmos I see them moving around just not their sprite

    And I know it because when I remove them from being an agent, they show up, but when I add the agent component it makes them disappear.
     
  8. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    maybe screenshot or gif/video would help..
    to see how the sprite is assigned,
    how camera is looking towards them,
    maybe sprite gets rotated with agent? (so that camera cannot see)
     
  9. QueenRosa

    QueenRosa

    Joined:
    May 10, 2023
    Posts:
    16

    Here's the settings + a video

    upload_2023-10-12_3-32-33.png


    the video: https://files.fm/u/j573na3a64

    If I remove the agent component, then reload the play scene, it will show up, but putting the agent component back will make it invisible (even after it sets UpAxis, etc to false).

    The agent is on the same layer as the camera renders (Default..) so it's not that.
     

    Attached Files:

  10. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    if you hide that ground, is it visible? (if sorting order somehow gets wrong value)
     
  11. QueenRosa

    QueenRosa

    Joined:
    May 10, 2023
    Posts:
    16
    Yeah hiding the map doesnt change anything sadly.

    Edit: For some weird reason, adding a navmeshagent component to the player fixed it?

    But now I have another issue, with the Agent component now attached to my player, i run around and I randomly get stuck. Just. Stop moving randomly. And I need to nudge one of the keys to keep moving again (but the running animation still plays). I'm using unity input movement system btw.
     
    Last edited: Oct 12, 2023
  12. QueenRosa

    QueenRosa

    Joined:
    May 10, 2023
    Posts:
    16