Point & Click to Move in Unity
In the current project, I’m doing which has a stealth element, we move by clicking the ground / Point & Click
Let’s start simple… We can simply create our Point & Click with Unity’s Navigation system also called NavMesh.
Baking The Floor
- Open your Navigation tab from Window -> AI -> Navigation
- Select the Ground Object and in the Navigation Tab click Bake
If you have your Gizmos on you should see that the ground’s color is blue
Making the Player move (with NavMesh and Raycast)
To make the player move we add a simple script to it and we use Raycast to define where we click with the mouse, then we take the position and use SetDestination to move our player to that position.
Before you do these steps, add a NavMesh Agent to your Player
- Create script and attach that to player (I created a simple “Player” script)
- We need a handle for our NavMeshAgent
- And set our Raycast
4. We create a method that we call when we want to move to the position which we just clicked.
- We set up the ray
- If we hit something with the ray, we move the information using out, to our hit
- Then inside the if statement we use SetDestination to make our agent’s new position to the hit.point. Which is the position we just clicked.
And in the update function, we call this method when we click.
And this is how we create a simple Point & Click with Unitys Navigation system. I hope this helped you on your coding/game dev journey! :)