Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NavMesh Agents Ignoring GameObject Scripts When Not Selected in Editor

Discussion in 'Navigation' started by Sengarden, May 3, 2021.

  1. Sengarden

    Sengarden

    Joined:
    Oct 8, 2020
    Posts:
    9
    Hello,
    I'm designing an infinite runner style game, but rather than using swappable tracks to move between, I want to be able to simulate a real populated street for the player to steer left and right across as the tiles / enemies / items move towards them. Something I know this is bound to require is an AI system with local avoidance so that enemies don't run into each other. I've been watching tutorials and reading what I can about Unity's NavMesh features, and it's working okay so far, but I've run into an issue I can't find a solution for anywhere.

    I've applied agents to two enemies in my sample scene, one moving west, representing a stray animal that would run out of an alleyway and sideways across the player's path, and the other moving south, representing a pedestrian moving up the street towards the player, so that I can experiment with obstacle avoidance variables.

    When I have the Stray selected in the editor, its scripting works excellently. Its code to make it transform.translate towards the player's z position at the same speed as the environment to simulate player movement executes just as it would if it wasn't using the agent, and it tracks the moving destination gameobject (also moving towards the player at the same speed, located in the alleyway across the street) without any issue. It navigates from A to B without rotating in any weird way, and in relation to the moving environment tiles, looks like it's just crossing the street. However, when I deselect the Stray, or select any other gameobject before or during runtime, the Stray seems to suddenly ignore its scripting to transform.translate towards the player's z position, slides along the edge of the navmesh in the alleyway as the house down the street pushes up against it, and is left behind once it leaves the alleyway by the houses whizzing past it towards the player. By this point, the Stray is now further down the street from the destination gameobject as an effect of ignoring its scripting, and is never able to catch up to it.

    Any ideas what could be causing this?
    Thanks so much!