I have a first person shooter, with my character “dude” and some enemies that spawn “enemy”. For some reason the enemies spawn and start moving but they move to where my character begins, not to where my character currently is.
Is there a way I could maybe query my characters position on every frame and change the enemies destination on every frame to match? I just don’t know why its not working- Any help would be great. Here is the script I’m using…
(also another detail is that if I have the original prefab in the scene it follows me properly while the spawned ones gather at the origin. All the variables are the same so I don’t know why theyre different)
var dude : GameObject;
var agent : NavMeshAgent;
function Start ()
{
//agent.destination = dude.transform.position;
agent.destination = dude.transform.TransformPoint(Vector3.zero);
}
function Update ()
{
//agent.destination = dude.transform.position;
agent.destination = dude.transform.TransformPoint(Vector3.zero);
}
Instead of using dude.transform.TransformPoint you can use dude.transform.position to set the destination. So use the code that is commented out in your code snippet:
var dude : GameObject;
var agent : NavMeshAgent;
function Start ()
{
agent.destination = dude.transform.position;
}
function Update ()
{
agent.destination = dude.transform.position;
}
this can be in two different ways, eaither all you enemys “ask” for the position each frame.
Or you could do it the other way around, that each frame your player objects “sends” it’s information to a certain “moveMentHandler” and all your enemys get the information from this movementHandler.
this could be a better option if you are planning on doing many updates and a lot of informationsending and such.
just remember that update() is called on each frame and lastUpdate() is called after Update has been called. So your player moves and sends it’s information in the "lastUpdate() function (so it sends i t AFTER it has moved) then your enemys get it in the update() function and updates its position.