This content originally appeared on Level Up Coding - Medium and was authored by Calum Slee

For this game, we have a somewhat top-down view of our Main Character, and want to be able to click on screen to move him around. To do so, we can make use of Raycasts.
Raycasts are part of Unity’s Physics System, and allow us to create straight lines (or rays) from one object to another. One function of Raycast is ScreenPointToRay which allows us to draw our Ray from the screen or camera position.
On checking if the left mouse button is clicked, we can draw a Ray from our Main Camera passing in the mouse position as it’s target.
The Physics System allows us to detect what the Ray hits and store this as a variable. This can be extremely useful when we want different things to occur depending on what is clicked. But for now, we simply just want to move our Player.

In this example, we are using Unity’s NavMeshAgent system, which will be covered in tomorrow’s article. But note that we are simply storing a Vector3 with the Hit variable’s point position, and then passing this into our SetDestination function of the NavMeshAgent.

Point and Click To Move! was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Calum Slee

Calum Slee | Sciencx (2021-08-22T20:21:38+00:00) Point and Click To Move!. Retrieved from https://www.scien.cx/2021/08/22/point-and-click-to-move/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.