- Home /
Can't get player to walk to object using NavMeshAgent
I've tried multiple tutorials and tried things I've come across online, but none of them has solved my issue. I'm using Unity 2021.1.3
My player has the NavMeshAgent component with mostly default settings.
My terrain is mostly default as well, except for the fact that I set it to "Navigation Static"
My navigation shows what I would expect for the NavMesh and I've baked it.
My code uses SetDestination() to move to the tapped object. I can confirm that the object tapped has the proper position being passed to it. I can also confirm that the playerAgent is my player gameobject' NavMeshAgent.
public NavMeshAgent playerAgent;
private float speed = 1f;
// Walk to and face the object - if within a range
public virtual void moveToInteraction(NavMeshAgent playerAgent)
{
this.playerAgent = playerAgent; // Need to set a NavMesh - refer to video 1
// playerAgent.destination = this.transform.position;
Debug.Log(this.transform.position.x.ToString() + " " + this.transform.position.z.ToString());
this.playerAgent.SetDestination(this.transform.position);
Interact();
}
public virtual void Interact()
{
Debug.Log("Interacting with base class - not the interacted object.");
}
When I tap to the object I want to walk to, my player will stay in place but all it does is rotate a little bit (about 90 degrees and not even in the direction of the object).
Edit: the rotation that occurs doesn't happen until I click on the object, so I know that the SetDestination is doing something - just not moving to it.
Answer by Patrickmol · Jun 16, 2021 at 07:51 AM
I remember about having the same problem a long time ago... I think I solved by calling SetDestination() method with less frequency because if you spamm it then it will stand still as I remember.... I am not sure, has been a long time since that.