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

Question I have to add a delay between disabling NavMeshAgent and setting rotation or it wont work?

Discussion in 'Scripting' started by mrCharli3, Oct 5, 2023 at 11:11 AM.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    946
    I have a NavMeshAgent that moves around, and when he sees player he rotates towards player and shoots.

    I tried implementing this like so:

    Code (CSharp):
    1. agent.enabled = false;
    2. transform.rotation = GetDirection();
    But for some reason 20% of the time, the rotation wouldnt get set properly.

    So I tried making it a Coroutine:

    Code (CSharp):
    1. agent.enabled = false;
    2.  
    3. yield return new WaitForSeconds(0.1f);
    4.  
    5. transform.rotation = GetDirection();
    Now it works every time, (yield return null did not work), what could be causing this? Rly dont want to use this ugly solution.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,638
    Been a while since I touched NavMesh stuff, but looking at the docs I see properties such as
    isStopped
    and
    updateRotation
    that might be a more direct means of allowing you to aim the agent. Maybe even just have the actual character as a child of the nav mesh agent and have it face in the right direction, being it the direction it's headed in, or towards a target.

    As to why this happens, probably just a race condition. I don't imagine agents are updated every frame, so potentially even when you disable it, it may still be in control of the transform for a frame or two.
     
    Nad_B likes this.
  3. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    270
    Yes adding the agent to a child game object like @spiney199 suggested would be the straightforward approach, if you don't want to deal with some (complex?) State just for rotation/movement coordination (IsMoving, IsRotating...)
     
  4. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    946
    I think he suggested adding the agent to the parent and having the mesh as a child, and controlling the mesh rotation, no?
     
  5. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    946
    ah yes maybe that would make things easier, so basically I rotate the child mesh (what you call character) so it’s visually facing the right dir, right?
     
  6. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    421
    When I made a game with agents I never let them have control of rotation. You often want them to look at the player or a collectable item. If there's nothing around to LookAt then make them look in the direction of travel, or scan/spin around looking for trouble.
     
  7. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    946
    That’s a nice idea actually. So u just disable agent rotation in Awake?
     
  8. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    421