Enemy stops where player spawn (Navigation help)
I baked a NavMesh and all that jazz and added a script for my "zombies" to go to my destination, but they just go to where I spawned and stand there?
My code on the enemy is:
public Transform Player;
void Update()
{
GetComponent<NavMeshAgent>().destination = Player.position;
}
They navigate correctly, avoiding obstacles and all, but just stop at my spawn position. The "Zombies" are prefabs if that matters and my player is also a prefab. Any help would be greatly appreciated!
Does your player position ever change? What is it exactly that you expect to happen?
Yes, my player is moving using WASD. I expect the enemy to follow the player as he moves around the Nav$$anonymous$$esh. Thanks for the reply! Hopefully I can get this figured out because it's annoying how many hours I've spent on it haha! @inso$$anonymous$$x
Your code looks like it should work. Is the value for Player.position changing? $$anonymous$$aybe the "Player" transform is the parent of thing doing the moving?
Answer by Taxen0 · Oct 30, 2015 at 07:41 PM
Try using the following:
public Transform Player;
private NavMeshAgent agent;
void Awake() {
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
agent.SetDestination(Player.position);
}
I would also recommend only to update when the player actually moves instead of in update.
Hope it helps!
I somehow resolved the issue by doing the following:
public Transform Player;
void Update()
{
Player = GameObject.FindGameObjectWithTag("Player").transform;
GetComponent<Nav$$anonymous$$eshAgent>().destination = Player.position;
}
Thank you both for you replies though!!
Your answer
![](https://koobas.hobune.stream/wayback/20220612043253im_/https://answers.unity.com/themes/thub/images/avi.jpg)