Click to Move Player in Unity

Thomas Mauro
3 min readJan 2, 2023

--

Side Note: You can get all the projects about my recent articles in completion on my GitHub. I put all the Raycasting up to this point in my GLI.Course Git. Everything is separated into different scenes and has the appropriate name to correlate with each project. There will be a few more as well after this.

Let's get a quick overview of how to Click to Move in Unity. Have you ever played that kind of game where you click to move? They are usually turned-based and/or action/turn-based/strategy-style RPG games. Here is a cool article I found about some of those types of games.

18 Classic Point & Click Games That Still Hold Up Well Today

Well, let's get into the basics of how to get this started. I know you will take it and show the world how amazing your creativity and programming skills are eventually.

  • Get two cubes into the scene, and turn one of them into a floor ( I named this scene Click to Move )
  • Adjust the Floor and Player ( Cubes )
  • Now let's make a script. I will call mine PointToMove
  • Put the script onto the Main Camera
  • Make a new player script for this challenge ( I have 0–3 so now it’s time to make a Player4 script ) This is because I have other projects in different scenes. You do whatever you want with yours. That will go onto the Player cube of course.
  • Don't forget to initialize using unityEngine.InputSydstem on both scripts.

Pointer Behaviour:

  • if left click cast out a ray
  • if hits the floor, set the target destination (tell the player to move here)
  • Make a variable to access the Player from the PointToMove script
  • FindObjectOfType in void Start

Player Behaviour:

  • In the update method, move toward the target
  • Make a variable to store a target destination
  • You can Tag the Player as a Player or just call for it via script type because there is only one player in this scene
  • create a new method for UpdateDestination
  • Calculate distance > (Vector3.Distance documentation)
  • I locked the y-axis to 0.55f so the Player won't go everywhere you point and it will work better on the floor as it should

Let's check it out in action.

Beautiful!!!!!!!!!!!!!

Next up I will be getting into A.I. See you later alligator.

--

--