Navmeshagent not working

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.

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.

2 Likes

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.

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.

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!

1 Like

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

void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
transform.position += Vector3.forward * vertical * Time.deltaTime;
transform.position += Vector3.right * horizontal * Time.deltaTime;
}

Then, on your player you put a NavmeshAgent and a new component with code like this:

public Transform target;
public NavMeshAgent agent;

private Vector3 lastPosition;
void Update()
{
if(target.position != lastPosition)
{
agent.SetDestination(target.position);
lastPosition = target.position;
}
}

and in the inspector you drag the player to the slot of the agent and the empty gameobject to target.

1 Like

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.

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.

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.

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.

5 Likes

For anyone who comes across this problem, make sure you have enabled “Auto Sync Transforms” in the physics section of your project settings. Edit>Project Settings>Physics>Auto Sync Transforms.

56 Likes

Ah, thank you takeaguess, I was having exactly the same problem (i think one of the Unity courses doesn’t work in 2019).

1 Like

Exactly what was needed takeaguessplease. For others getting stuck in the same course; some search terms for posterity:

Humble Unity Bundle
Ultimate Guide to Game Development 2019
Navigation Mesh

5 Likes

Thank you so much for your help!! I am using Unity 2020, I have solve this problem for you

1 Like

Thank you! i was starting to think i wouldn’t around this. I am also following the udemy course Ultimate Guide to Game Development 2019.

Thank you!! i have the same problem that samy cash, this solution were perfect and instant applied.

Thank you so much, saved me a lot of time. To everyone else, yes I was using the same course, and I did install Unity 2017 to fix other errors.

This actually solved the problem.

You are a legend!!! Thank you very much.

I

I hope you have found the solution?