Point & Click Movement in Unity

This article will show how to move the Player to the point clicked on the screen. Implementing a move-to-point clicked system is relatively simple.

For this, since we will use a Raycast, the Floor GameObject will need a Collider.

Next, we create a script and attach it to the Player GameObject.

In the Player script, we need three variables, one for the speed the Player will move at, the second to hold the position of the clicked point, and the last to know if the Player has reached the end.

In Update, when the left mouse button is pressed, we create a Ray. A Ray is an infinite line that starts at an origin and goes in some direction. In this case, the Ray’s origin is the point on the screen where the mouse is, with the direction the camera is facing. Then we send out a Raycast that will hit a collider along the Ray, return the hit information if there was a Collider to hit, store the location of the hit, and tell the Player that it has not reached the point.

Now, to move towards the point and let the Player know when it has reached the end.

The Player now moves to the clicked point on the screen.

In the following article, we will look at using the NavMesh with the Point and Click movement.

Unity Developer, Software Engineer, Game Designer