Building A Stealth Game — Using The Mouse For Movement

Joshua Nielsen
6 min readFeb 24, 2022

How to use the mouse as input for a 3rd person perspective game

Photo by Pixabay from Pexels

Today we move on from the broad topic of cinematography to the more focused task of building a simple stealth game in Unity. A “stealth game” is a game where we seek to achieve our objectives by avoiding contact with the enemies rather than fight them.

This particular game will be a variation on “The Great Fleece” project from GameDevHQ. This project is a collection of assets put together with the express purpose of being the aid for learning cinematography and building a basic stealth game.

In this article, we’re going to learn how to use the mouse for movement in a Unity 3D environment.

Preparing the environment

The first thing we need to have player movement is an environment for the player to move in. This involves “baking” a NavMesh to make use of Unity’s navigation features.

I don’t want to get too deep into the navigation features of Unity, as it is a large topic and I am no where near an expert in it, but here’s some basic to get started.

The basic necessities to creating the NavMesh is to first find all of the objects in the scene that would affect the navigation — walking surfaces and obstacles, for example. Once these objects are discovered, they must be set to “static”.

The “Static” checkbox is in the upper right corner

Next, find the Navigation window in the Unity editor, and then find the Bake tab within the window.

The Bake tab of the Navigation window

There are some settings here that can be configured based on the attributes of the “agent” (the object that will be using the navigation system). Once these are set, we can then press the Bake button to set the NavMesh of the scene.

Setting up the player

The next thing we need is to set up the player object, or whatever it is that we want the player to be able to move. This could just be a simple 3D object if necessary.

Once we have our player object in the game, we then need to add to it the Nav Mesh Agent component. This component gives the object a bit of AI in that it guides the object on the best path to its destination.

The Nav Mesh Agent component

As you can see, there are a number of options to configure on the Nav Mesh Agent. Feel free to play around with those, but for now the default options are fine.

In addition to the Nav Mesh Agent, we will also need a custom script for the player object. Create a new script and attach it to the player object. The rest of our work will be done in scripting.

Getting the mouse input

Within our script, we first want to capture when the player clicks with their mouse. We also need to be watching for this act at all times, so we need this code to go into the script’s Update method.

Within that method, we can watch for input from the player by using the command Input.GetMouseButtonDown. This is a method that takes an integer parameter that represents which of the mouse’s buttons we are considering. In this case, we will use zero, which represents the left mouse button.

The method then returns a boolean value representing whether or not the specified mouse button has been clicked. So we’ll want all of our logic for player movement to be contained within an if statement that is continually checking for player input.

Determining the click point

Once we know when the player has clicked, we next need to know where they clicked. For that, we will make use of Unity Raycasts. Raycasts are sort of an invisible “ray” that shoots forth from a specific location and then proceeds in a perfectly straight line. Using them we can get the information we’re looking for.

To make use of Raycasts, we need three things:

  1. A Ray — Data type that represents a ray in Unity.
  2. A RaycastHit — Data type used to get the information from a Raycast.
  3. The Raycast function — The Physics function that will generate the data.

The Ray we will get from a function of the main camera — ScreenPointToRay. This function takes a Vector3 position as a parameter and returns a Ray.

The RaycastHit need only be declared. The Raycast function will be populating it with data.

Finally, the Raycast function is part of the Physics library. It takes two parameters — a Ray and a RaycastHit, though the RaycastHit is an out parameter. It returns a boolean value that represents whether or not the Ray intersects with a Collider. This is important, because if this function returns false than there will be no RaycastHit information.

We should now have a script within our if statement something like this:

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

RaycastHit raycastHit;

if (Physics.Raycast(ray, out raycastHit)) {}

With that, we should have an Update method that whenever the player clicks their mouse button, our Raycast code gets information about where they clicked on the screen.

Moving the player

Now that we know where we want the player object to move to, we can work to make that finally happen. For that, we will be using the Nav Mesh Agent we added to the player object earlier.

First we will need to have access to the Nav Mesh Agent in our script, so make that available to the script in the way you prefer. One good way is to make a serialized field for the Nav Mesh Agent and add it in the Unity Editor.

Once we have access to the Nav Mesh Agent, it’s very simple to make it move. All we need is a single line of code in our script. Within the if statement for the Raycast function, add a call to the Nav Mesh Agent method SetDestination.

SetDestination takes one Vector3 parameter representing the target destination. The method returns a boolean value representing whether or not the method succeeded in setting a destination.

Our target parameter for SetDestination will be the point property of our RaycastHit variable. This represents the impact point where the ray hit a collider. In other words, it’s where the player clicked and where they would like to move to.

With the SetDestination method in place, the player object now moves to its intended destination when the player clicks their mouse.

Conclusion

And that does it. Simply setting a (valid) destination for the Nav Mesh Agent will then trigger Unity’s navigation features to cause the player object to move, navigate around obstacles, and stop at the specified destination.

By using the Nav Mesh Agent, we can actually make anything navigate the environment, even without input from a player. And we’re going to explore that idea in the next article. Until then, thanks for reading.

--

--