Point & Click to Move in Unity
Today we will learn how to make a point and click system for moving our player.
So when we click a left mouse button we will cast a ray from the mouse position to the floor. We will know the hit position, and in the next article, we will learn how to use the Navmesh system to move our player to that position.
Create a new script called Player and add it to your Player GameObject.
The first thing we need to do is check if the mouse button was clicked. To do that we will use Input.GetMouseButtonDown(0). the number in the brackets tells us which button we are checking. 0 — left mouse button.
Next, we need to cast a ray from the screen position to the world position. For that, we need to create a new variable.
Now we need to cast a ray and check if we hit something. Let’s check the documentation to see how to do that.
In the if statement we are checking the hit position of our ray, we are creating a cube and setting the position of that cube to the hit position.
In the next article, I will show you how to move our Player to that position with a Navmesh system.