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.

Resolved NavMesh Agents going through obstacles

Discussion in 'Navigation' started by MinhocaNice, Dec 8, 2022.

  1. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    I am creating a simple FPS game and the zombies are controlled using NavMesh agents. Initially, I thought that if I added colliders in the scene that were not shown in the NavMesh, the agents would simply be unable to detect and would collide with it, staying there for eternity. This would be helpful to me since I made a script that makes the zombies attack when they notice a destructible object in front of them, meaning they will destroy obstacles.

    However, that didn't work out as I thought it would. It seems agents are somehow able to phase through any obstacles not mentioned in the NavMesh, which is really odd. I tried adding a
    OnCollisionEnter
    so they disabled when colliding with something, but that didn't work out as the method is never called.
    OnTriggerEnter
    seems to not exist in Unity's system despite being mentioned by some forums online as a solution to that.

    So what do I do? How can I make so agents actually with everything even if it's not mentioned in the NavMesh?
     
    Last edited: Dec 10, 2022
  2. Geejayz

    Geejayz

    Joined:
    Feb 8, 2016
    Posts:
    72
    Hi,
    You are correct, navmeshagents will not take part in collisions - you need to use the physics system for that.
    Try adding a Rigidbody Component to your Zombie, together with a Capsule Collider component. Let's say you have a crate in the path of the Zombie and its destination - that should also have a collider. If you add an OnCollisionEnter method within a script on either the zombie or the crate it should now detect the collision. You will then need logic within that method as to what will happen next. It's possible/likely that the zombie will continually bump against the crate as it continues trying to reach its destination. You could perhaps disable the navmeshagent, run an attacking animation, destroy your crate and then reinstate the navmeshagent again.

    Note: Your crate should not be marked as a static object or a navmeshobject as these would mean the bake would simply mark them as obstacles and the zombie would move around them.

    Hope that all makes sense.

    (OnTriggerEnter would also require one of the colliding objects to have a rigidbody, and the collider itself would need to be marked as a Trigger on its component).
     
  3. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    I specified that I tried
    OnCollisionEnter
    and
    OnTriggerEnter
    and neither worked. The former is never called while the later doesn't show up as a viable method using the default libraries (what library contains
    OnTriggerEnter
    ?).

    EDIT: I was able to use
    OnTriggerEnter
    , turns out that using the wrong parameters make the method be redefined as a custom one instead of just giving an error and since I had the
    (Collision collision)
    parameters still from
    OnCollisionEnter
    it didn't load the other method. However, even after making
    OnTriggerEnter
    work it's still never called just like
    OnCollisionEnter
    , even when the agent is literally phasing straight through a wall. I know this because I used
    print
    to see if it was being called or not.
     
    Last edited: Dec 10, 2022
  4. Geejayz

    Geejayz

    Joined:
    Feb 8, 2016
    Posts:
    72
    And you definintely have a Rigidbody on one of the two colliding objects? That should be the only other component you need to guarantee collisions.
     
  5. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    Yes. The zombies have rigidbodies.
     
  6. WalterPalladino

    WalterPalladino

    Joined:
    Oct 24, 2016
    Posts:
    11
    Maybe executing periodically a BoxCast you could add the behavior you are looking for.
     
  7. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    Sorry the late reply.

    Do I really need to go after that to add such feature? This feels like a really makeshift solution to something that would have been better handled by
    OnCollisionEnter
    and
    OnTriggerEnter
    . Doesn't anyone know what can cause those methods to not be called when they should (already checked layers)?
     
  8. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    Marked this as resolved and moved discussion to another thread.