WEBVTT 00:00:00.000 --> 00:00:01.931 As a general rule, if you're going to have 00:00:01.931 --> 00:00:03.931 moving game objects in your game 00:00:03.931 --> 00:00:05.058 you should make sure that they are 00:00:05.058 --> 00:00:06.685 rigidbody objects. 00:00:07.185 --> 00:00:09.185 Rigidbodies are components that allow 00:00:09.185 --> 00:00:11.365 a game object to be effected by physics. 00:00:11.365 --> 00:00:13.365 They allow the object to fall under gravity, 00:00:13.365 --> 00:00:15.809 and have physics properties such as mass, 00:00:15.809 --> 00:00:17.406 drag and velocity. 00:00:17.906 --> 00:00:19.423 When we add a rigidbody component 00:00:19.423 --> 00:00:22.029 to a game object we often then refer to it 00:00:22.029 --> 00:00:23.808 as a rigidbody object. 00:00:24.128 --> 00:00:26.183 A rigidbody component is required for any physics 00:00:26.183 --> 00:00:28.337 based interaction, and the game object 00:00:28.337 --> 00:00:30.337 must also have a collider attached 00:00:30.337 --> 00:00:33.059 in order to interact with other physics objects. 00:00:33.227 --> 00:00:35.840 Without a rigidbody our power cube will 00:00:35.840 --> 00:00:37.394 simply hover in mid air. 00:00:37.604 --> 00:00:39.604 But let's see what happens when we add one. 00:00:39.828 --> 00:00:41.677 Like any other component it can be added 00:00:41.677 --> 00:00:43.277 using the Add Component button at 00:00:43.277 --> 00:00:45.444 at the bottom of the inspector, or from 00:00:45.444 --> 00:00:46.941 the 'Component' top menu. 00:00:47.123 --> 00:00:49.123 You will find it under the Physics section. 00:00:51.129 --> 00:00:53.129 Now our object falls under gravity 00:00:53.129 --> 00:00:55.129 and can be controlled by the physics engine 00:00:55.129 --> 00:00:57.129 and any forces that are applied to it. 00:00:57.449 --> 00:00:59.449 Rigidbodies have numerous options. 00:00:59.645 --> 00:01:01.840 Firstly there are settings to control the mass, 00:01:01.840 --> 00:01:04.312 drag and angular drag of the game object. 00:01:04.312 --> 00:01:06.409 The mass of the object effects how collisions 00:01:06.409 --> 00:01:08.006 are treated with the object. 00:01:08.299 --> 00:01:10.299 Game objects with a higher mass will react 00:01:10.299 --> 00:01:12.467 less when collided with a lower mass 00:01:12.467 --> 00:01:13.433 game object. 00:01:13.671 --> 00:01:15.574 The drag of a game object effects how 00:01:15.574 --> 00:01:17.200 quickly it will slow down without 00:01:17.200 --> 00:01:18.269 other interactions. 00:01:18.507 --> 00:01:20.410 Think of it like air resistance. 00:01:20.634 --> 00:01:22.717 It's used to determine the rate of a loss 00:01:22.717 --> 00:01:24.213 of linear velocity. 00:01:24.492 --> 00:01:26.629 Similarly, angular drag effects how 00:01:26.629 --> 00:01:28.379 quickly the game object will slow it's 00:01:28.379 --> 00:01:30.379 angular velocity, i.e. how 00:01:30.379 --> 00:01:31.931 fast it is rotating. 00:01:32.196 --> 00:01:34.154 So for example if you're adding torque 00:01:34.154 --> 00:01:35.904 to the object to rotate it, 00:01:35.904 --> 00:01:37.904 the angular drag will create resistance 00:01:37.904 --> 00:01:40.110 to this force. The next option is 00:01:40.110 --> 00:01:41.433 whether or not the game object is 00:01:41.433 --> 00:01:43.071 effected by gravity. 00:01:43.532 --> 00:01:46.522 By enabling this checkbox we use gravity. 00:01:47.602 --> 00:01:49.608 Settings for gravity can be seen in the 00:01:49.608 --> 00:01:54.419 Edit - Project Settings - Physics area of Unity. 00:01:55.122 --> 00:01:57.513 As you can see it's a 3 dimensional vector 00:01:57.513 --> 00:01:59.126 which by default has a real world 00:01:59.126 --> 00:02:01.557 value of -9.81. 00:02:02.027 --> 00:02:04.441 Because you can customise it globally here 00:02:04.441 --> 00:02:06.880 you could also create interesting effects 00:02:07.380 --> 00:02:09.655 Such as low gravity for a platformer 00:02:09.655 --> 00:02:11.655 or even setting it to a different axis 00:02:11.655 --> 00:02:13.082 as part of a puzzle game. 00:02:13.181 --> 00:02:16.065 For example, let's add gravity to the 00:02:16.065 --> 00:02:18.065 Z axis by a value of 5. 00:02:20.250 --> 00:02:22.134 And now the power cube is pulled towards 00:02:22.134 --> 00:02:24.134 towards the global Z axis. 00:02:25.905 --> 00:02:28.417 The Is Kinematic setting effects whether 00:02:28.417 --> 00:02:30.925 or not a rigid body will react to physics. 00:02:31.113 --> 00:02:33.782 Ordinarily when a scene begins, all static 00:02:33.782 --> 00:02:36.969 geometry, meaning any non-rigidbody objects 00:02:36.969 --> 00:02:39.202 are checked once by the physics engine 00:02:39.202 --> 00:02:41.202 and not checked again for efficiency. 00:02:41.202 --> 00:02:43.202 However when you move a static object 00:02:43.202 --> 00:02:45.966 the physics engine must re-check all other 00:02:45.966 --> 00:02:48.383 static objects for the sake of accuracy, 00:02:48.621 --> 00:02:50.621 and this can be expensive to performance. 00:02:51.047 --> 00:02:53.752 To avoid this, Kinematic rigidbody objects can be used 00:02:53.752 --> 00:02:55.419 and moved via their transform 00:02:55.419 --> 00:02:57.282 by using the Translate function. 00:02:57.546 --> 00:02:59.546 This means that you can have physics objects 00:02:59.546 --> 00:03:02.950 that effect others but are not effected themselves. 00:03:03.229 --> 00:03:05.229 An obvious example of this would be the 00:03:05.229 --> 00:03:08.018 paddle in a Pong or Breakout style game. 00:03:08.753 --> 00:03:11.778 In this example our rigidbody power cube 00:03:11.778 --> 00:03:13.778 has Use Gravity checked. 00:03:14.154 --> 00:03:17.420 When we press play, the object falls to the ground. 00:03:18.238 --> 00:03:21.349 We also have our round prop samoflange ball 00:03:21.349 --> 00:03:23.349 object, which has a similar component setup. 00:03:24.650 --> 00:03:27.220 If the power cube does not have gravity 00:03:28.922 --> 00:03:30.922 then it will not fall under it, but it will 00:03:30.922 --> 00:03:32.783 be effected by other objects. 00:03:33.242 --> 00:03:35.061 If we don't want it to be effected by other 00:03:35.061 --> 00:03:38.313 objects we can use Is Kinematic. 00:03:42.142 --> 00:03:44.543 And as stated we can also move the object 00:03:44.543 --> 00:03:46.833 via it's transform. So we'll make use of 00:03:46.833 --> 00:03:49.274 this simple script, which uses the 00:03:49.274 --> 00:03:51.801 translate function to move it via it's 00:03:51.801 --> 00:03:54.549 forward direction every frame. 00:03:56.858 --> 00:03:59.387 And as you can see, the object still 00:03:59.387 --> 00:04:02.084 interacts with the others but remains a 00:04:02.084 --> 00:04:04.447 rigidbody, so is constantly informing 00:04:04.447 --> 00:04:06.280 the physics engine of it's location 00:04:06.280 --> 00:04:08.128 and not forcing the physics engine 00:04:08.128 --> 00:04:10.128 to re-evaluate the entire scene. 00:04:10.628 --> 00:04:13.041 The Interpolate and Extrapolate settings 00:04:13.041 --> 00:04:14.859 are there to solve jittering. 00:04:14.986 --> 00:04:16.723 If you experience slight movement of your 00:04:16.723 --> 00:04:19.221 object when moving it via it's rigidbody, 00:04:19.221 --> 00:04:21.451 make use of the interpolate setting in order to 00:04:21.451 --> 00:04:23.827 smooth the transform movement based on the 00:04:23.827 --> 00:04:26.061 previous frame. And the extrapolate setting 00:04:26.061 --> 00:04:27.866 to smooth based on a predicted 00:04:27.866 --> 00:04:29.368 location in the next frame. 00:04:29.368 --> 00:04:31.094 The next setting is for the type of 00:04:31.094 --> 00:04:33.527 collision detection. We have Discrete, 00:04:33.527 --> 00:04:35.527 Continuous and Continuous Dynamic. 00:04:36.085 --> 00:04:38.002 The default is discrete and unless you 00:04:38.002 --> 00:04:40.542 have any problems you should use discrete. 00:04:41.125 --> 00:04:43.125 Continuous is for fast moving objects 00:04:43.125 --> 00:04:46.053 that are interacting with static geometry. 00:04:46.318 --> 00:04:48.944 And continuous dynamic is for fast moving 00:04:48.944 --> 00:04:50.820 objects that are interacting with other 00:04:50.820 --> 00:04:52.388 dynamic objects. 00:04:52.971 --> 00:04:54.846 Finally the constraints section of the 00:04:54.846 --> 00:04:56.846 rigidbody component allow you to 00:04:56.846 --> 00:04:58.846 constrain movement or rotation of the object 00:04:58.846 --> 00:05:00.846 by physics. For example, if you 00:05:00.846 --> 00:05:02.846 had a Tetris style game you might not 00:05:02.846 --> 00:05:04.846 want the cubes of your game to rotate 00:05:04.846 --> 00:05:07.168 as they fell in to place. You could constrain 00:05:07.168 --> 00:05:09.502 this using the rotation constraints here. 00:05:10.193 --> 00:05:12.635 In this example our power cube is 00:05:12.635 --> 00:05:15.026 falling on to the workbench. It's a rigid 00:05:15.026 --> 00:05:17.026 body that has Use Gravity checked. 00:05:17.995 --> 00:05:19.995 And as standard it falls like this. 00:05:20.302 --> 00:05:23.130 If we didn't want it to rotate as it falls 00:05:23.130 --> 00:05:26.053 we can freeze the rotation within the constraints. 00:05:26.553 --> 00:05:29.815 And now when it falls, no rotation.