Simple Click To Move In Unity

Ben Pielstick
May 6, 2021

--

Player movement can be accomplished in any number of ways, but one of the most basic is by simply clicking the mouse. While this isn’t quite as simple as WASD in Unity, it’s still pretty easy using the built in navigation features.

First we need to add a Nav Mesh Agent component to our player, which will allow our player to move along a surface.
Next we have to actually bake our nav mesh on our walkable surface. This should work so long as your object is set to Navigation Static.
Once the game objects are ready, it’s only a few lines of code to get click to move working. Just a ray cast from the mouse pointer position to get a destination, then set the navigation to work.

That’s really all there is to it. There are of course more settings you can configure, and if you need a more advanced solution you could use a third party package or even write your own solution, but this is enough for something basic. My recommendation if you need click to move is to give this setup a try, and be sure you actually need a more robust pathfinding solution before you reach for one, as the built-in features may be enough to get the job done.

--

--

Ben Pielstick

Game developer with over 12 years of experience, mainly focused on MMOs.