Unity3D: Learn How to Point and Click to Move!
Would you like to learn how to move your player by pointing and clicking?
Objective
The objective is to move the player around by pointing anywhere on the ground and move the player towards where we clicked with the left mouse button. This requires a bit of setup in Unity, but overall, an easy feature to install.
Setup
The first thing we want to do is open the Navigation system.
The navigation system window will open. Dock it next to the inspector if it isn’t.
Select the floor game object you wish the player to move on.
In the navigation panel, under the “Object” tab, click on the Navigation Static box. Make sure your floor game object is selected.
Next, select the “Bake” tab in the navigation window.
Press the “Bake” button down below to bake the floor.
It will turn the floor blue, as seen below. It represents where the player can move when we set up the point and click feature.
Next, we want to add a Nav Mesh Agent to our player. In this case, the capsule is our player.
Select your player game object. Click on “Add Component” and look for “Nav Mesh Agent.”
Once added, we can now create the player script.
Player Script
In your script folder, create a new script and name it “Player.”
Open the script so we can now add logic for the point and click feature!
Since we need a reference to the player’s nav mesh agent, we need to add the namespace:
using UnityEngine.AI;
It will allow us to access the nav-mesh-agent.
[SerializeField] private NavMeshAgent _agent = null;
In the Update() function, we want to add the logic for the input and movement of the player.
private void Update()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
_agent.SetDestination(hit.point);
}
}
}
If the player clicks on the left mouse button, we want to fire a ray toward where we place the mouse cursor. So we need to store the main camera to access the ability to screen point a ray using the mouse position.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
We also need to store the location “where we clicked” into a “RaycastHit” variable called hit
.
We then check to see if hit
has any data for the player to move to. If it does, we then need to call a function within our nav mesh agent variable, _agent.SetDestination()
, and pass in the hit variable as hit.point
.
_agent.SetDestination(hit.point);
The point is the impact point in world space where the ray hit the collider. In other words, it is where we clicked on the floor.
The player script should look like this:
using UnityEngine;
using UnityEngine.AI;public class Player : MonoBehaviour
{
[SerializeField] private NavMeshAgent _agent = null;private void Update()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
_agent.SetDestination(hit.point);
}
}
}
}
And that wraps up the coding part!
Let's head back to Unity to add some final touches.
We need to add the player script to our player game object.
We also need to reference our nav mesh agent.
That is all! We are done! Finito!
Conclusion
We now have a working point and click system at its rudimentary level!
To show that I am clicking on the ground, I add logic to instantiate a sphere game object at the hit.point
location.
I hope this will help you in some way!
That is all for today. Thank you for your time!
Gabriel