Point and Click Movement in Unity!

Objective: Implement point and click movement into your game!

Point and click movement is a method in some games where you click on an area of a game and the character will walk to that point. Together we are going to implement this form of movement.

Create and/or open your player script. Inside we are going to need to use Raycasting. Raycasting is going to allow us to cast from our mouse position to the floor, get a position that we hit, and we are going to tell the character he needs to move to that position.

In order to move your player you have to look up how to do that through the NavMesh Agent, because they are stuck to the NavMesh so you can only move THRUGH the NavMesh.

First, you want to get the position of where you hit. Under the update method we need to see if we clicked.

With mouse buttons, 0 is left click, 1 is right click, and 2 is mouse wheel!

We need to create a variable to store the ray.

Now, where ever you click you will cast a ray. you want to make sure that what ever you hit you want to return what you hit.

What ever you hit will be stored in the hit info

you need to create an if statement that check if you hit something. You can set up a debug for the floor for where you hit.

Point means the impact point in world space where the ray hit the collider.

It’s time to move the character to the point where you clicked. You need to create a handle for the NavMesh Agent.

Now to move the NavMesh Agent you need to set the NavMesh destination with the hit info point.

you will also need a vector3 variable and set the variable to the hit info point. This will move you character to the target.

Now you have a working point and click movement system. You can make adjustments to your characters movements if you need, such as stop distance or movement speed. Don’t be afraid to experiment with your code and I’ll see you in the next tutorial!