< Return to Video

Raycasting - Unity Official Tutorials

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

more » « less
Video Language:
English
Duration:
04:47

English subtitles

Revisions