Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Navmeshagent not working

Discussion in 'Navigation' started by sammy_kash, Mar 17, 2019.

  1. sammy_kash

    sammy_kash

    Joined:
    Mar 17, 2019
    Posts:
    5
    I have a navmesh baked into a level, and a navmeshagent attached to a player object, I'm not able to figure out how to keep the player stuck to the baked area so he does not fall off the map. help please.
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    242
    That's how NavMeshAgents are supposed to work, unless you are moving your player without actually using the Agent.
    You need to use agent.SetDestination(target), to properly use the agent, instead of doing the usual transform.position= or rigidbody.position= . Think of it like using a point-and-click movement system, like in Diablo.
    You can "hack" a regular movement control system together limited tot he NavMesh by moving a target object with your input, and then have your player set the agent's destination to that target everytime it changes.
     
    sammy_kash likes this.
  3. sammy_kash

    sammy_kash

    Joined:
    Mar 17, 2019
    Posts:
    5
    Thanks for the reply, I am following a course on Udemy. And that's how he shows .
    And I understood your explanation but how do I add it to to my player in FPS so that he doesn't go off of the nav mesh ? If this isn't the way, what's the other way ? I basically want my player to not fall off the map.
     
  4. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    242
    Like I ended my post with, you can hack something like this by moving an invisible target object with your controls like you normally would (transform.position = ) and then have your NavMeshAgent walk to the target object with Agent.SetDestination(target object.position) everytime the target object gets changed.
     
  5. sammy_kash

    sammy_kash

    Joined:
    Mar 17, 2019
    Posts:
    5
    I'm sorry, I'm just getting started and a complete amateur in this. I didn't quite get you, if you could tell it in any other way or refrenece the code or a video it would be really helpful. Thank you!
     
  6. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    242
    I'm gonna go step by step how you can do this very simply. When you get more experience in programming you will probably want to make this code a little "safer", but to get things up and running quickly this should do it.
    All the code here is written quickly off the top of my head, so you might need to fix a few names here and there.

    First, create a new empty gameobject. Give this object a component with
    Code (CSharp):
    1. void Update()
    2. {
    3. float horizontal = Input.GetAxis("Horizontal");
    4. float vertical = Input.GetAxis("Vertical");
    5. transform.position += Vector3.forward * vertical * Time.deltaTime;
    6. transform.position += Vector3.right * horizontal * Time.deltaTime;
    7. }
    Then, on your player you put a NavmeshAgent and a new component with code like this:
    Code (CSharp):
    1. public Transform target;
    2. public NavMeshAgent agent;
    3.  
    4. private Vector3 lastPosition;
    5. void Update()
    6. {
    7. if(target.position != lastPosition)
    8. {
    9. agent.SetDestination(target.position);
    10. lastPosition = target.position;
    11. }
    12. }
    and in the inspector you drag the player to the slot of the agent and the empty gameobject to target.
     
  7. sammy_kash

    sammy_kash

    Joined:
    Mar 17, 2019
    Posts:
    5
    Thank you for your patience and time, I tried it exactly as you have mentioned, but I ended up locking my mouselook.
    So yeah it didn't work and I'm still falling of the map, and I couldn't figure out why. Thank you for the help though, I'll try to figure out something later.
     
  8. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    242
    That's strange, unless you perhaps changed your input horizontal and vertical inputs to respond to your mouse movement? By default those inputs should correspond to the arrow or wasd keys.
     
  9. sammy_kash

    sammy_kash

    Joined:
    Mar 17, 2019
    Posts:
    5
    I know, and no I didn't . I put it in the player script .I have different scripts for mouse look and its not really a lock ,it's more like ,when I move the mouse, the position is coming back to the same initial position.
     
  10. RogelBR

    RogelBR

    Joined:
    Apr 11, 2019
    Posts:
    1
    I had the same problem...
    I have find 2 solutions to this.

    Replace character controller with navmeshagent in your player script. ( Dont forget to put "using UnityEngine.AI;" library needed to use navmeshagent )

    or use unity 2017.