[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.00,0:00:02.10,Default,,0000,0000,0000,,Raycasting is the process of shooting Dialogue: 0,0:00:02.10,0:00:04.28,Default,,0000,0000,0000,,an invisible ray from a point, in a Dialogue: 0,0:00:04.28,0:00:06.77,Default,,0000,0000,0000,,specified direction to detect whether any Dialogue: 0,0:00:06.77,0:00:08.77,Default,,0000,0000,0000,,colliders lay in the path of the ray. Dialogue: 0,0:00:08.77,0:00:10.77,Default,,0000,0000,0000,,One such example of this would be Dialogue: 0,0:00:10.77,0:00:12.87,Default,,0000,0000,0000,,shooting a gun. In this instance our Dialogue: 0,0:00:12.87,0:00:14.87,Default,,0000,0000,0000,,character wants to shoot the evil box that Dialogue: 0,0:00:14.87,0:00:16.87,Default,,0000,0000,0000,,betrayed him and killed his father. Dialogue: 0,0:00:17.37,0:00:20.10,Default,,0000,0000,0000,,The syntax of the raycast function looks like this. Dialogue: 0,0:00:20.10,0:00:22.10,Default,,0000,0000,0000,,It can be confusing at first Dialogue: 0,0:00:22.10,0:00:24.53,Default,,0000,0000,0000,,but once you understand what each part does Dialogue: 0,0:00:24.53,0:00:26.20,Default,,0000,0000,0000,,it makes much more sense. Dialogue: 0,0:00:26.20,0:00:28.20,Default,,0000,0000,0000,,Firstly, the origin of the ray is a Dialogue: 0,0:00:28.20,0:00:30.20,Default,,0000,0000,0000,,point in world space. Dialogue: 0,0:00:30.20,0:00:32.20,Default,,0000,0000,0000,,So in this instance we'd choose a point in Dialogue: 0,0:00:32.20,0:00:34.20,Default,,0000,0000,0000,,front of the barrel of the gun, stored Dialogue: 0,0:00:34.20,0:00:37.58,Default,,0000,0000,0000,,as a Vector3, an X, Y and Z position. Dialogue: 0,0:00:37.92,0:00:39.92,Default,,0000,0000,0000,,However, because our world coordinates Dialogue: 0,0:00:39.92,0:00:41.93,Default,,0000,0000,0000,,direction won't be facing in the direction Dialogue: 0,0:00:41.93,0:00:44.42,Default,,0000,0000,0000,,we're shooting we will need a second Vector3 Dialogue: 0,0:00:44.42,0:00:46.21,Default,,0000,0000,0000,,to store our direction in. Dialogue: 0,0:00:46.21,0:00:48.21,Default,,0000,0000,0000,,These two Vector3 variables make up Dialogue: 0,0:00:48.21,0:00:51.16,Default,,0000,0000,0000,,our ray. But we can also substitute Dialogue: 0,0:00:51.16,0:00:53.69,Default,,0000,0000,0000,,in a Ray variable, as this data type Dialogue: 0,0:00:53.69,0:00:55.69,Default,,0000,0000,0000,,can store two Vector3's. Dialogue: 0,0:00:55.69,0:00:57.69,Default,,0000,0000,0000,,Our code would then look like this. Dialogue: 0,0:01:02.38,0:01:04.20,Default,,0000,0000,0000,,The next argument in the function is a Dialogue: 0,0:01:04.20,0:01:06.20,Default,,0000,0000,0000,,RaycastHit variable that stores Dialogue: 0,0:01:06.20,0:01:08.14,Default,,0000,0000,0000,,information on the colliders hit. Dialogue: 0,0:01:08.45,0:01:10.45,Default,,0000,0000,0000,,So that it can be queried in code as to which Dialogue: 0,0:01:10.45,0:01:12.85,Default,,0000,0000,0000,,objects are intersected by the ray. Dialogue: 0,0:01:13.15,0:01:15.61,Default,,0000,0000,0000,,Finally there are two optional arguments, Dialogue: 0,0:01:15.61,0:01:17.61,Default,,0000,0000,0000,,Distance, which defines the length Dialogue: 0,0:01:17.61,0:01:19.61,Default,,0000,0000,0000,,of the ray, if omitted the ray will default Dialogue: 0,0:01:19.61,0:01:21.61,Default,,0000,0000,0000,,to an infinite length. Dialogue: 0,0:01:21.61,0:01:23.61,Default,,0000,0000,0000,,And Layer Mask. This is the number Dialogue: 0,0:01:23.61,0:01:26.55,Default,,0000,0000,0000,,of a particular layer in Unity's layer system Dialogue: 0,0:01:26.55,0:01:28.55,Default,,0000,0000,0000,,on which you can place objects if you Dialogue: 0,0:01:28.55,0:01:30.22,Default,,0000,0000,0000,,wish to make the ray ignore them. Dialogue: 0,0:01:30.22,0:01:32.22,Default,,0000,0000,0000,,Let's look at another practical example of Dialogue: 0,0:01:32.22,0:01:33.51,Default,,0000,0000,0000,,using raycasting. Dialogue: 0,0:01:33.51,0:01:35.89,Default,,0000,0000,0000,,In this example we have a parachute crate Dialogue: 0,0:01:35.89,0:01:37.36,Default,,0000,0000,0000,,that opens a parachute when it's Dialogue: 0,0:01:37.36,0:01:39.20,Default,,0000,0000,0000,,nearing the floor. Dialogue: 0,0:01:40.46,0:01:42.26,Default,,0000,0000,0000,,The crate is made up of two parts, Dialogue: 0,0:01:42.26,0:01:44.03,Default,,0000,0000,0000,,the chute and the crate itself. Dialogue: 0,0:01:44.03,0:01:46.03,Default,,0000,0000,0000,,The chute has two animations Dialogue: 0,0:01:46.53,0:01:48.04,Default,,0000,0000,0000,,one to open the chute Dialogue: 0,0:01:48.60,0:01:50.03,Default,,0000,0000,0000,,and another to close it. Dialogue: 0,0:01:50.92,0:01:52.92,Default,,0000,0000,0000,,In this example we need to cast a ray Dialogue: 0,0:01:52.92,0:01:55.33,Default,,0000,0000,0000,,downwards in order to see how far the crate is Dialogue: 0,0:01:55.33,0:01:57.33,Default,,0000,0000,0000,,from the floor, and we check for the Dialogue: 0,0:01:57.33,0:02:00.12,Default,,0000,0000,0000,,floor by looking for the environment collider. Dialogue: 0,0:02:00.62,0:02:03.15,Default,,0000,0000,0000,,Our collider for the environment is tagged Dialogue: 0,0:02:03.15,0:02:04.69,Default,,0000,0000,0000,,with the word environment. Dialogue: 0,0:02:05.40,0:02:09.20,Default,,0000,0000,0000,,And in our script we are looking for that tag. Dialogue: 0,0:02:10.88,0:02:12.48,Default,,0000,0000,0000,,The RayCast function gets placed inside Dialogue: 0,0:02:12.48,0:02:15.66,Default,,0000,0000,0000,,an IF statement so that if it returns true, Dialogue: 0,0:02:15.66,0:02:17.66,Default,,0000,0000,0000,,meaning if it intersects with anything, Dialogue: 0,0:02:17.66,0:02:19.66,Default,,0000,0000,0000,,then the comments within the IF statement Dialogue: 0,0:02:19.66,0:02:21.66,Default,,0000,0000,0000,,will be carried out and the RayCastHit Dialogue: 0,0:02:21.66,0:02:25.24,Default,,0000,0000,0000,,variable can be queried as to what has been hit. Dialogue: 0,0:02:26.24,0:02:29.18,Default,,0000,0000,0000,,So within an IF statement we've written Dialogue: 0,0:02:29.18,0:02:32.86,Default,,0000,0000,0000,,Physics.Raycast, we have a landingRay variable Dialogue: 0,0:02:32.86,0:02:35.71,Default,,0000,0000,0000,,that's storing the position of the box Dialogue: 0,0:02:35.71,0:02:37.71,Default,,0000,0000,0000,,and a downward direction. We're using Dialogue: 0,0:02:37.71,0:02:39.71,Default,,0000,0000,0000,,the shortcut Vector3.down, Dialogue: 0,0:02:39.71,0:02:41.96,Default,,0000,0000,0000,,and we're using this as the ray to cast. Dialogue: 0,0:02:41.96,0:02:44.37,Default,,0000,0000,0000,,Our RaycastHit variable - 'hit' - Dialogue: 0,0:02:44.37,0:02:46.37,Default,,0000,0000,0000,,is storing anything that gets hit by the Dialogue: 0,0:02:46.37,0:02:48.37,Default,,0000,0000,0000,,ray as it is cast downwards, Dialogue: 0,0:02:48.37,0:02:50.37,Default,,0000,0000,0000,,and the distance, or 'length' or the ray Dialogue: 0,0:02:50.37,0:02:53.41,Default,,0000,0000,0000,,is defined by our 'deployment height' variable. Dialogue: 0,0:02:53.41,0:02:55.86,Default,,0000,0000,0000,,If the ray intersects with a collider Dialogue: 0,0:02:58.86,0:03:01.80,Default,,0000,0000,0000,,then we call the deploy parachute function. Dialogue: 0,0:03:02.30,0:03:04.76,Default,,0000,0000,0000,,This function then simply sets our Boolean Dialogue: 0,0:03:04.76,0:03:08.19,Default,,0000,0000,0000,,'deployed' flag to true so that this cannot repeat. Dialogue: 0,0:03:08.69,0:03:11.02,Default,,0000,0000,0000,,And then we set the drag of the rigid body Dialogue: 0,0:03:11.02,0:03:13.90,Default,,0000,0000,0000,,to the variable 'parachuteEffectiveness'. Dialogue: 0,0:03:13.90,0:03:15.90,Default,,0000,0000,0000,,So we slow down the crate as if it's being Dialogue: 0,0:03:15.90,0:03:17.58,Default,,0000,0000,0000,,held up by the parachute. Dialogue: 0,0:03:17.58,0:03:19.58,Default,,0000,0000,0000,,We also play the animation Dialogue: 0,0:03:19.58,0:03:21.58,Default,,0000,0000,0000,,on the parachute object, Dialogue: 0,0:03:21.58,0:03:23.58,Default,,0000,0000,0000,,which is a game object that we'll assign Dialogue: 0,0:03:23.58,0:03:25.16,Default,,0000,0000,0000,,to the public variable. Dialogue: 0,0:03:25.36,0:03:27.94,Default,,0000,0000,0000,,We then have a separate OnCollisionEnter function Dialogue: 0,0:03:27.94,0:03:29.94,Default,,0000,0000,0000,,which simply plays the closing animation. Dialogue: 0,0:03:29.94,0:03:31.94,Default,,0000,0000,0000,,So we know that as soon as it hits the ground Dialogue: 0,0:03:31.94,0:03:34.72,Default,,0000,0000,0000,,or another object the parachute can close. Dialogue: 0,0:03:36.44,0:03:39.57,Default,,0000,0000,0000,,So here we've set the length of the ray to 4 Dialogue: 0,0:03:39.57,0:03:41.81,Default,,0000,0000,0000,,by setting 4 as our deployment height Dialogue: 0,0:03:42.03,0:03:45.42,Default,,0000,0000,0000,,And we're setting the drag of the rigidbody to 8 Dialogue: 0,0:03:45.67,0:03:48.62,Default,,0000,0000,0000,,by setting the parachute effectiveness to 8. Dialogue: 0,0:03:48.62,0:03:50.62,Default,,0000,0000,0000,,And we've simply dragged our parachute Dialogue: 0,0:03:50.62,0:03:54.27,Default,,0000,0000,0000,,chute object on to the parachute variable. Dialogue: 0,0:03:54.27,0:03:57.12,Default,,0000,0000,0000,,Because this is the object that has an animation Dialogue: 0,0:03:57.12,0:03:59.12,Default,,0000,0000,0000,,component in order to playback Dialogue: 0,0:03:59.12,0:04:01.12,Default,,0000,0000,0000,,it's opening and closing animations. Dialogue: 0,0:04:01.12,0:04:03.12,Default,,0000,0000,0000,,So let's see that play one more time. Dialogue: 0,0:04:06.68,0:04:08.68,Default,,0000,0000,0000,,It's also worth keeping in mind Dialogue: 0,0:04:08.68,0:04:10.68,Default,,0000,0000,0000,,that although you cannot see Dialogue: 0,0:04:10.68,0:04:13.09,Default,,0000,0000,0000,,raycasts drawn in the scene view Dialogue: 0,0:04:13.09,0:04:15.49,Default,,0000,0000,0000,,or in the game. You can also use the Dialogue: 0,0:04:15.49,0:04:18.01,Default,,0000,0000,0000,,Debug.DrawRay function Dialogue: 0,0:04:18.01,0:04:21.05,Default,,0000,0000,0000,,in order to preview where a ray would be going. Dialogue: 0,0:04:21.05,0:04:23.50,Default,,0000,0000,0000,,By adding Debug.DrawRay Dialogue: 0,0:04:23.50,0:04:25.50,Default,,0000,0000,0000,,we're drawing a visual ray from Dialogue: 0,0:04:25.50,0:04:27.50,Default,,0000,0000,0000,,the position of the box in the direction Dialogue: 0,0:04:27.50,0:04:30.08,Default,,0000,0000,0000,,of Vector3.down, multiplied by Dialogue: 0,0:04:30.08,0:04:33.73,Default,,0000,0000,0000,,the deployment height - the length of our existing ray. Dialogue: 0,0:04:33.73,0:04:35.73,Default,,0000,0000,0000,,And by doing this we've matched the actual Dialogue: 0,0:04:35.73,0:04:38.24,Default,,0000,0000,0000,,ray that we're casting in the IF statement below. Dialogue: 0,0:04:38.55,0:04:40.55,Default,,0000,0000,0000,,So when we play this back you can see that Dialogue: 0,0:04:40.55,0:04:42.55,Default,,0000,0000,0000,,Unity demonstrates the ray Dialogue: 0,0:04:42.55,0:04:45.89,Default,,0000,0000,0000,,by showing us the drawn ray in the scene view.