Simple point and click controls

Point and click controls are very popular in both, adventure games and action RPG’s. These types of controls are quite straightforward to set up in Unity. Let’s go over how to do this.

For this to work we will need something called a navmesh baked into the scene. The navmesh will map out the area for pathfinding, i.e where the player and NPCs can walk. Ensure that any object that needs to be an obstacle is set as static before baking, otherwise the navmesh will not detect it and the player can walk straight through it. Open the Navigation window and click on Bake. When the baking has finished, you will see the floor covered by the blue navmesh:

Create the Player script and attach to the player object. Also add a Navmesh Agent component to the player object. I recommend setting Angular Speed and Acceleration very high to make sure the controls feel responsive:

Open up the Player script and add the using statement “UnityEngine.AI” which is needed to access the NavMeshAgent class. Initialise NavMeshAgent in the Start function:

Add a new function called MoveToPosition. In this function call ScreenPointToRay which returns a ray based on mouse position. Store the result in a local Ray. Also add a RaycastHit to store the actual hit information. Inside the if statement set the destination of the agent based on where the ray hits. Lastly, call MoveToPosition from the Update function when left mouse is clicked:

Good luck!

Unity / C# Game developer