How can I use a navmesh with keyboard input to move an object?
There seem to be several tutorials for using navmesh to move a player with the mouse, but I want to use the keyboard arrow keys? Do I need to calculate the position to move too then use GetComponent().destination = calculated position?
Answer by KaavayGupta · Dec 04, 2019 at 01:56 PM
float horInput = Input.GetAxis("Horizontal");
float verInput = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(horInput, 0f, verInput);
Vector3 moveDestination = transform.position + movement;
GetComponent<NavMeshAgent>().destination = moveDestination;
Put this code in the Update function.
We are basically getting the input. Storing it in Vector 3. Calculating the position to move and store it as a Vector 3. And updating the navmeshagent's destination.
I know this is an old post, but man you made my week! such a simple solution in the end.
You can also use .Move(moveDestination)
instead of setting the destination directly.
Your answer
Follow this Question
Related Questions
Nav Mesh Agent rotation 0 Answers
How to change speed for a cloned prefab instead of applying it to all instances of prefab? 1 Answer
SetDestination doesn't work after building (with images) answer please 0 Answers
Help With NavMesh 0 Answers
NavMesh working but not allowing me to deal damage to the enemy 0 Answers