Creating Point & Click Player Movement By Using Raycasting And A Nav Mesh In Unity

Point and click controls are a classic staple that we see in a lot of games. Diablo was the forerunner of this in a lot of ways by creating an combat oriented RPG that revolved around clicking on an enemy and then watching your character run up to them and hit them.

Today we will be creating a form of character controller that utilizes mouse clicks to move the player. Creating this is actually quite simple, but requires a few steps first.

If you haven’t seen my articles “How To Use Raycasting To Detect Collisions In Unity With C#” as well as “How To Create And Utilize A NavMesh In Unity”, now would be the time to do so.

Now after creating your Navigational Mesh and baking it in your scene, you can add the “Nav Mesh Agent” component to your player. This is what we are going to call on in order to move the player through code, similar to what we would do with a character controller.

The namespace “using UnityEngine.AI” MUST be used in order to access all of the AI properties in Visual Studios such as those used for Raycasting and the NavMeshAgent.

Notice how the player character (*capsule*) moves along the floor from its location directly to the point I click. This is happening because in order for the Nav Mesh to take obstacles into account, I will have to mark them as “Static” within the inspector and re-bake my Nav Mesh.

And now with the objects changed to static, the player capsule will detect them and navigate around them when moving.

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!