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

Top Down Sprite with the NavMeshAgent Issues

Discussion in '2D' started by alphamalet, Dec 29, 2013.

  1. alphamalet

    alphamalet

    Joined:
    May 1, 2013
    Posts:
    2
    I am attempting to make a top down 2D shooter with sprites, and need help getting everything to work correctly. My game will need pathfinding for its AI, and to accomplish this I am trying to use the Nav Mesh Agent. Doing this with Unity's 2D perspective appears to be impossible, since the Nav Mesh won't bake on a plane parallel to the Y axis. What I am trying to do instead is have a fixed top-down camera on the Y axis, and have my Game take place entirely on the X and Z axis(depicted in my pictures). This way I can bake a NavMesh and use the Nav Mesh Agent for my AI.

    To accomplish this I am using a GameObject with a box collider, a sprite renderer, and a NavMeshAgent

    Unfortunately I am not getting the desired results.

    Prior to playing the scene, it looks like this. Everything is the way it should be.
    $Screen shot 2013-12-29 at 6.30.34 PM.png

    When I play the scene, the sprite, and it's collider rotate perpendicularly to the ground, looking like this.
    $Screen shot 2013-12-29 at 6.32.08 PM.png

    Pathfinding behaves the way I intended it to by my script, but I don't want the NavMeshAgent to rotate my sprite renderer.

    Can anyone tell me how to stop the NavMeshAgent from rotating my GameObject, so that my sprite stays at the desired perspective.

    I have tried manually setting the GameObject's rotation, and that doesn't seem to work. I have also tried using this line of code in my script...

    GetComponent<NavMeshAgent>().updateRotation = false;

    ...and that doesn't seem to work either. Are there any suggestions? The game I'm making needs to be made in a crunch, so programming my own pathfinding isn't really an option, and I really want to pathfind through the NavMeshAgent since that's what I'm familiar with.
     
  2. alphamalet

    alphamalet

    Joined:
    May 1, 2013
    Posts:
    2
    This issue has been solved. I posted this question on reddit, and a user by the name of Finblast managed to find a solution for me. His response:

    Ah, finally a question I'm qualified to answer! (I'm currently making a top down game that is setup exactly the same way.) I quickly replicated your setup and I think I know what the problem is.
    You can't use the sprite renderer component on the same GO [Game Object] as the navmeshagent, since it's default rotation makes it face the Z-axis. You probably tried to rotate your GO around the X-axis by 90 degrees when setting it up to get it facing the camera. The thing is the navmeshagent component requires your GO's Y-axis to always point away from the floor, so when you play your scene it will reset any rotations you made in the inspector, if the Y-axis isn't pointing away from the floor normals.
    The solution is pretty simple. Make a new GameObject that handles the sprite rendering, instead of putting it in the same GO as the navmeshagent. Then make this new GO a child of the GO that has the navmeshagent on it. This way you can rotate it any way you like, and the navmeshagent won't reset it's rotation.
    EDIT: You probably don't want to mess around with NavMeshAgent.updateRotation, I think that only affects the Y-axis rotation, meaning if it's set to false your AI will never turn to face anything other than the initial direction.
     
    Offlein, gamefish and oakus like this.