Point and click movement in Unity

Daniel Kirwan
2 min readJun 16, 2021

--

Objective: Move a player to a point in the game world using Unity navmesh

Above is the end result of setting up simple point and click movement system in Unity.

First thing to do is to set up your navmesh in Unity. I clicked on my flooring in my hierarchy and then open the navigation window. Then selected the bake tab in the navigation window and click bake. Depending how big you want your navmesh to be, this could take some time.

Another point to mention is that the cabinets that are attached to the floor need to be made static in the scene view. This means that the navmesh will work around around the static objects and not allow anything moving along the navmesh to pass through it.

Now getting to the point and click movement. First, add a navmesh agent to the player.

Now in your player movement script, we need to add in the AI library and then create a navmesh agent.

We then need to set up the point and click movement.

I am setting the movement to the left mouse button. If a click is detected then I set up a Ray to be the point in the game world space and then set up a Raycast.

Then, if a raycast detects a hit I then use the SetDestination method that is using Unity’s navigation AI. You need to use a vector3 for the destination and it just so happens that our hit.point is exactly where we want to go.

And that is it for simple movement using a navmesh.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet