-
Raycasting is the process of shooting
-
an invisible ray from a point, in a
-
specified direction to detect whether any
-
colliders lay in the path of the ray.
-
One such example of this would be
-
shooting a gun. In this instance our
-
character wants to shoot the evil box that
-
betrayed him and killed his father.
-
The syntax of the raycast function looks like this.
-
It can be confusing at first
-
but once you understand what each part does
-
it makes much more sense.
-
Firstly, the origin of the ray is a
-
point in world space.
-
So in this instance we'd choose a point in
-
front of the barrel of the gun, stored
-
as a Vector3, an X, Y and Z position.
-
However, because our world coordinates
-
direction won't be facing in the direction
-
we're shooting we will need a second Vector3
-
to store our direction in.
-
These two Vector3 variables make up
-
our ray. But we can also substitute
-
in a Ray variable, as this data type
-
can store two Vector3's.
-
Our code would then look like this.
-
The next argument in the function is a
-
RaycastHit variable that stores
-
information on the colliders hit.
-
So that it can be queried in code as to which
-
objects are intersected by the ray.
-
Finally there are two optional arguments,
-
Distance, which defines the length
-
of the ray, if omitted the ray will default
-
to an infinite length.
-
And Layer Mask. This is the number
-
of a particular layer in Unity's layer system
-
on which you can place objects if you
-
wish to make the ray ignore them.
-
Let's look at another practical example of
-
using raycasting.
-
In this example we have a parachute crate
-
that opens a parachute when it's
-
nearing the floor.
-
The crate is made up of two parts,
-
the chute and the crate itself.
-
The chute has two animations
-
one to open the chute
-
and another to close it.
-
In this example we need to cast a ray
-
downwards in order to see how far the crate is
-
from the floor, and we check for the
-
floor by looking for the environment collider.
-
Our collider for the environment is tagged
-
with the word environment.
-
And in our script we are looking for that tag.
-
The RayCast function gets placed inside
-
an IF statement so that if it returns true,
-
meaning if it intersects with anything,
-
then the comments within the IF statement
-
will be carried out and the RayCastHit
-
variable can be queried as to what has been hit.
-
So within an IF statement we've written
-
Physics.Raycast, we have a landingRay variable
-
that's storing the position of the box
-
and a downward direction. We're using
-
the shortcut Vector3.down,
-
and we're using this as the ray to cast.
-
Our RaycastHit variable - 'hit' -
-
is storing anything that gets hit by the
-
ray as it is cast downwards,
-
and the distance, or 'length' or the ray
-
is defined by our 'deployment height' variable.
-
If the ray intersects with a collider
-
then we call the deploy parachute function.
-
This function then simply sets our Boolean
-
'deployed' flag to true so that this cannot repeat.
-
And then we set the drag of the rigid body
-
to the variable 'parachuteEffectiveness'.
-
So we slow down the crate as if it's being
-
held up by the parachute.
-
We also play the animation
-
on the parachute object,
-
which is a game object that we'll assign
-
to the public variable.
-
We then have a separate OnCollisionEnter function
-
which simply plays the closing animation.
-
So we know that as soon as it hits the ground
-
or another object the parachute can close.
-
So here we've set the length of the ray to 4
-
by setting 4 as our deployment height
-
And we're setting the drag of the rigidbody to 8
-
by setting the parachute effectiveness to 8.
-
And we've simply dragged our parachute
-
chute object on to the parachute variable.
-
Because this is the object that has an animation
-
component in order to playback
-
it's opening and closing animations.
-
So let's see that play one more time.
-
It's also worth keeping in mind
-
that although you cannot see
-
raycasts drawn in the scene view
-
or in the game. You can also use the
-
Debug.DrawRay function
-
in order to preview where a ray would be going.
-
By adding Debug.DrawRay
-
we're drawing a visual ray from
-
the position of the box in the direction
-
of Vector3.down, multiplied by
-
the deployment height - the length of our existing ray.
-
And by doing this we've matched the actual
-
ray that we're casting in the IF statement below.
-
So when we play this back you can see that
-
Unity demonstrates the ray
-
by showing us the drawn ray in the scene view.