Point & Click Move in Unity

The time has come for this project to start working on the gameplay. So let’s start with the Player handling. For this game instead of a free movement, I chose a great classic of pc games. The point & click system, among the most famous games that use this system, one of my favorites Monkey Island.

To realize it I decided to get help from Unity using its navigation system, the Navmesh, as a system is very suitable for AI in fact, as we will see to use it we should use the library of artificial intelligence written in Unity.

For this first approach, it’s just fine. So let’s start with how to add navmesh to our game scene. First, we need to open its tab that we find under Windows>AI>Navigation.

Now we must select the game object that represents the surface that will be navigable and click on “Bake” and we are already at a good point in our work.

Before moving the player, however, if you have objects in the scene that are not to be crossed/triggered by the player or other elements that will move, you should make them static. You should make them static to prevent this from happening.

We’ll talk more about this later, but basically, you should know that the navmesh allows us to calculate the shortest path to get to the desired point so if an object is not defined as an obstacle it will pass through it instead of going around it.

Let’s move on to the player, first of all, the player must have an essential component to move on the navmesh, the “Navmesh Agent” and finally we should write a script that will use this component.

Finally, we go into the script to use the navmesh classes we need to extend the Artificial Intelligence library.

The goal is to take the position of the point where you click with the mouse in the game and move the player to that position. The movement to the final position will be handled by the navmesh by calling the SetDestination method that needs Vector3 as a parameter.

To calculate the point we have to use a Ray that defines the actual position of where we clicked with the mouse.

Now using a Raycast we define the point where our ray hit and transform it into a string to send to our Navmesh Agent.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store