Point & Click to Move in Unity — Part 2: Creating a Raycast from Mouse Click
Detecting player click
Objective: create a raycast and detect the position of the click of the player.
Before show you how to create the functionality here I left you the links to the previous articles related to this topic:
Ok, in this case I create a Player c# script and added it to the player game object.
Then I wrote a pseudocode with the steps to follow to create the functionality.
After that, in order to create the function first I used the function Input.GetMouseClickDown(0) to check if the player clicked the left button of the mouse. Then I created a Ray variable and assigned a ray from the position of the main camera to the player click.
Next, I create a RaycastHit variable to store the hit info and a if statement to check if the ray cast hit an object (passing the hit veritable to store the hit info of that raycast). And the last step was to create a game object in the clicked position by the user
And that’s it!!! We have the ray cast ready and we know the position of the click of the player.
One extra thing to clean up a little bit was to erase the object instantiation of the code and leave a comment to know what to do later.