How to make a NavMeshAgent move in the direction it is looking?
I have a navmesh agent I'm trying to move off another gameobject below it. I'm trying to move it forward in whichever direction it is looking by telling the agent to move in front of where the eyes are looking. This code moves the agent in a direction but not where it is looking. How can I make the agent move the direction the eyes of the agent are looking? Thanks
public float distance = 10f;
void OnTriggerStay(Collider c)
{
c.GetComponent<StateController> ().agent.destination = c.transform.position + c.GetComponent<StateController> ().eyes.forward * distance;
}
Answer by Dizy · Jun 20, 2017 at 08:03 AM
NavMeshAgent are especially usefull when you make a RTS-like game. Setting the destination of your units allow them to use the built-in path finding algorithme.
Using it this way seems strange for me but here is the solution :
void OnTriggerStay(Collider c) {
float distance = 10;
c.GetComponent<StateController> ().agent.destination = c.transform.position + c.GetComponent<StateController>().eyes.forward * distance;
}
(Not Tested)
In fact you say to your NavMeshAgent to move forward, corresponding to Vector3(0, 0, 1). You need to specify the distance or the movement will be almost invisible.
Edit : Nevermind, i've not seen the "OnTriggerStay", well if you need to move forward OnTriggerStay it make sens... just use this :
c.transform.position + c.GetComponent<StateController>().eyes.forward
As Woltus said
@Dizy You were right you need to specify the distance for the agent to travel or else it will not move at all.
Answer by tongtheluong · Jun 20, 2017 at 01:32 AM
What do you mean? Do you need an agent finding target? so it must be SetDestination
I have a navmesh agent and the only way I know how to move it is setting the destination. But what should I do if I just want to tell it to move forward? Theres no easy way to tell it just to move forward it seems while using the navmeshagent.destination because you have to set the destination to some type of gameobject in the scene for it to move to it. Transform.forward moves the agent but it doesn't move the agent where the eyes of the agent are pointing. I'm looking for a good way to tell the agent to move forward to where the eyes are looking currently as right now the agent is moving to a random spot not where the eyes were pointing.
Answer by rina81 · Jun 23, 2017 at 04:16 AM
I have the same problem. @DJGhostViper : Have you find a solution?
@rina81 The above code is corrected with the answer but here it is again, modify the distance to deter$$anonymous$$e how far you want the agent to move forward. ("Eyes" in the code is just an empty gameobject in front of the agent so you can tell where the agent is looking). Enjoy!
public float distance = 10f;
void OnTriggerStay(Collider c)
{
c.GetComponent<StateController> ().agent.destination = c.transform.position + c.GetComponent<StateController> ().eyes.forward * distance;
}
Answer by Woltus · Jun 20, 2017 at 07:44 AM
Try use:
void OnTriggerStay(Collider c)
{
void sc = c.GetComponent<StateController> ();
sc.agent.destination = c.transform.position + sc.eyes.forward;
}