Working with Unity Raycast
Learning how to move a character via Point and Click
In this Unity Raycast challenge, we will be working player movement using a point to move function via raycasting. Each Raycast position will translate to a new player position and the player must move towards that locations.
First we are going to create a floor like structure using a 3D cube then a player like object. (use an 3D cylinder with a red material)
Next we are going to create a new C# script called “point to move” and attached it to the Main Camera. Then we are going a 2nd C# script called “Player Movement” and attach this to the Player game object.
The game logic is going to very simple to understand, first we are going to have the Raycast logic within the point 2 move script. This will contain all the raycast hit Info and mouse position location. The information will then be pass to the Player movement script which will allow the Player game object to move towards the set location the Raycast was cast in.
To start we shall work with the Raycasting in the script “point 2 move” first. Just like how we setup the previous raycasting scripts, we will follow the same procedures in getting our Ray and RayCastHit.
The only difference this time is that we will need some script communication where you need to pass along position data to the Player Movement script. Therefore we will need a Serialized Field to gain access to the Player Movement script.
Before we can finish off the Point 2 Move script we will need to set up the Player Movement Script. What we need a public method that can send data (a Vector3 value) to the point and move script.
I named this public method “Movement” which will also require a Vector3 variable “Target Location”.
Then I am going to pass that Vector3 data to a local variable called “Target Destination” so the Player script can use it.
Finally we are going to a basic vector3 value subtracting form another vector3 value to “distance”. This is the conditional value which we set in an IF statement. If the distance between the current object (aka Player) and the Target location (Ray Cast Location) is greater than 0.1 then we will start to move towards that location.
To move towards that location we will use the basic format of “Transform Translate” then have a new local variable called “direction” which will contain the Target Destination (aka Ray Cast Location) subtract the Current Position (current Player position). This will keep occurring until the set distance is less than 0.1.
Back to the point and Move script we can finally pass in the Vector3 value to the Player Movement script. First we will need to gain access to the player movement script.
Then inside the Raycast physics IF statement, we will access the public method “Movement” that is part of the Player Movement script. Since the Player Movement method requires a Vector3 value, we will pass in our “hitInfo.point” value (since that is also a Vector3 value)
Back In Unity after you have slotting in the required game objects into your scripts and set the appropriate speed value. When testing, you will see now that the player will move towards the last clicked position.