< Return to Video

Physics Objects (Rigidbodies) - Unity Official Tutorials

  • 0:00 - 0:02
    As a general rule, if you're going to have
  • 0:02 - 0:04
    moving game objects in your game
  • 0:04 - 0:05
    you should make sure that they are
  • 0:05 - 0:07
    rigidbody objects.
  • 0:07 - 0:09
    Rigidbodies are components that allow
  • 0:09 - 0:11
    a game object to be effected by physics.
  • 0:11 - 0:13
    They allow the object to fall under gravity,
  • 0:13 - 0:16
    and have physics properties such as mass,
  • 0:16 - 0:17
    drag and velocity.
  • 0:18 - 0:19
    When we add a rigidbody component
  • 0:19 - 0:22
    to a game object we often then refer to it
  • 0:22 - 0:24
    as a rigidbody object.
  • 0:24 - 0:26
    A rigidbody component is required for any physics
  • 0:26 - 0:28
    based interaction, and the game object
  • 0:28 - 0:30
    must also have a collider attached
  • 0:30 - 0:33
    in order to interact with other physics objects.
  • 0:33 - 0:36
    Without a rigidbody our power cube will
  • 0:36 - 0:37
    simply hover in mid air.
  • 0:38 - 0:40
    But let's see what happens when we add one.
  • 0:40 - 0:42
    Like any other component it can be added
  • 0:42 - 0:43
    using the Add Component button at
  • 0:43 - 0:45
    at the bottom of the inspector, or from
  • 0:45 - 0:47
    the 'Component' top menu.
  • 0:47 - 0:49
    You will find it under the Physics section.
  • 0:51 - 0:53
    Now our object falls under gravity
  • 0:53 - 0:55
    and can be controlled by the physics engine
  • 0:55 - 0:57
    and any forces that are applied to it.
  • 0:57 - 0:59
    Rigidbodies have numerous options.
  • 1:00 - 1:02
    Firstly there are settings to control the mass,
  • 1:02 - 1:04
    drag and angular drag of the game object.
  • 1:04 - 1:06
    The mass of the object effects how collisions
  • 1:06 - 1:08
    are treated with the object.
  • 1:08 - 1:10
    Game objects with a higher mass will react
  • 1:10 - 1:12
    less when collided with a lower mass
  • 1:12 - 1:13
    game object.
  • 1:14 - 1:16
    The drag of a game object effects how
  • 1:16 - 1:17
    quickly it will slow down without
  • 1:17 - 1:18
    other interactions.
  • 1:19 - 1:20
    Think of it like air resistance.
  • 1:21 - 1:23
    It's used to determine the rate of a loss
  • 1:23 - 1:24
    of linear velocity.
  • 1:24 - 1:27
    Similarly, angular drag effects how
  • 1:27 - 1:28
    quickly the game object will slow it's
  • 1:28 - 1:30
    angular velocity, i.e. how
  • 1:30 - 1:32
    fast it is rotating.
  • 1:32 - 1:34
    So for example if you're adding torque
  • 1:34 - 1:36
    to the object to rotate it,
  • 1:36 - 1:38
    the angular drag will create resistance
  • 1:38 - 1:40
    to this force. The next option is
  • 1:40 - 1:41
    whether or not the game object is
  • 1:41 - 1:43
    effected by gravity.
  • 1:44 - 1:47
    By enabling this checkbox we use gravity.
  • 1:48 - 1:50
    Settings for gravity can be seen in the
  • 1:50 - 1:54
    Edit - Project Settings - Physics area of Unity.
  • 1:55 - 1:58
    As you can see it's a 3 dimensional vector
  • 1:58 - 1:59
    which by default has a real world
  • 1:59 - 2:02
    value of -9.81.
  • 2:02 - 2:04
    Because you can customise it globally here
  • 2:04 - 2:07
    you could also create interesting effects
  • 2:07 - 2:10
    Such as low gravity for a platformer
  • 2:10 - 2:12
    or even setting it to a different axis
  • 2:12 - 2:13
    as part of a puzzle game.
  • 2:13 - 2:16
    For example, let's add gravity to the
  • 2:16 - 2:18
    Z axis by a value of 5.
  • 2:20 - 2:22
    And now the power cube is pulled towards
  • 2:22 - 2:24
    towards the global Z axis.
  • 2:26 - 2:28
    The Is Kinematic setting effects whether
  • 2:28 - 2:31
    or not a rigid body will react to physics.
  • 2:31 - 2:34
    Ordinarily when a scene begins, all static
  • 2:34 - 2:37
    geometry, meaning any non-rigidbody objects
  • 2:37 - 2:39
    are checked once by the physics engine
  • 2:39 - 2:41
    and not checked again for efficiency.
  • 2:41 - 2:43
    However when you move a static object
  • 2:43 - 2:46
    the physics engine must re-check all other
  • 2:46 - 2:48
    static objects for the sake of accuracy,
  • 2:49 - 2:51
    and this can be expensive to performance.
  • 2:51 - 2:54
    To avoid this, Kinematic rigidbody objects can be used
  • 2:54 - 2:55
    and moved via their transform
  • 2:55 - 2:57
    by using the Translate function.
  • 2:58 - 3:00
    This means that you can have physics objects
  • 3:00 - 3:03
    that effect others but are not effected themselves.
  • 3:03 - 3:05
    An obvious example of this would be the
  • 3:05 - 3:08
    paddle in a Pong or Breakout style game.
  • 3:09 - 3:12
    In this example our rigidbody power cube
  • 3:12 - 3:14
    has Use Gravity checked.
  • 3:14 - 3:17
    When we press play, the object falls to the ground.
  • 3:18 - 3:21
    We also have our round prop samoflange ball
  • 3:21 - 3:23
    object, which has a similar component setup.
  • 3:25 - 3:27
    If the power cube does not have gravity
  • 3:29 - 3:31
    then it will not fall under it, but it will
  • 3:31 - 3:33
    be effected by other objects.
  • 3:33 - 3:35
    If we don't want it to be effected by other
  • 3:35 - 3:38
    objects we can use Is Kinematic.
  • 3:42 - 3:45
    And as stated we can also move the object
  • 3:45 - 3:47
    via it's transform. So we'll make use of
  • 3:47 - 3:49
    this simple script, which uses the
  • 3:49 - 3:52
    translate function to move it via it's
  • 3:52 - 3:55
    forward direction every frame.
  • 3:57 - 3:59
    And as you can see, the object still
  • 3:59 - 4:02
    interacts with the others but remains a
  • 4:02 - 4:04
    rigidbody, so is constantly informing
  • 4:04 - 4:06
    the physics engine of it's location
  • 4:06 - 4:08
    and not forcing the physics engine
  • 4:08 - 4:10
    to re-evaluate the entire scene.
  • 4:11 - 4:13
    The Interpolate and Extrapolate settings
  • 4:13 - 4:15
    are there to solve jittering.
  • 4:15 - 4:17
    If you experience slight movement of your
  • 4:17 - 4:19
    object when moving it via it's rigidbody,
  • 4:19 - 4:21
    make use of the interpolate setting in order to
  • 4:21 - 4:24
    smooth the transform movement based on the
  • 4:24 - 4:26
    previous frame. And the extrapolate setting
  • 4:26 - 4:28
    to smooth based on a predicted
  • 4:28 - 4:29
    location in the next frame.
  • 4:29 - 4:31
    The next setting is for the type of
  • 4:31 - 4:34
    collision detection. We have Discrete,
  • 4:34 - 4:36
    Continuous and Continuous Dynamic.
  • 4:36 - 4:38
    The default is discrete and unless you
  • 4:38 - 4:41
    have any problems you should use discrete.
  • 4:41 - 4:43
    Continuous is for fast moving objects
  • 4:43 - 4:46
    that are interacting with static geometry.
  • 4:46 - 4:49
    And continuous dynamic is for fast moving
  • 4:49 - 4:51
    objects that are interacting with other
  • 4:51 - 4:52
    dynamic objects.
  • 4:53 - 4:55
    Finally the constraints section of the
  • 4:55 - 4:57
    rigidbody component allow you to
  • 4:57 - 4:59
    constrain movement or rotation of the object
  • 4:59 - 5:01
    by physics. For example, if you
  • 5:01 - 5:03
    had a Tetris style game you might not
  • 5:03 - 5:05
    want the cubes of your game to rotate
  • 5:05 - 5:07
    as they fell in to place. You could constrain
  • 5:07 - 5:10
    this using the rotation constraints here.
  • 5:10 - 5:13
    In this example our power cube is
  • 5:13 - 5:15
    falling on to the workbench. It's a rigid
  • 5:15 - 5:17
    body that has Use Gravity checked.
  • 5:18 - 5:20
    And as standard it falls like this.
  • 5:20 - 5:23
    If we didn't want it to rotate as it falls
  • 5:23 - 5:26
    we can freeze the rotation within the constraints.
  • 5:27 - 5:30
    And now when it falls, no rotation.
Title:
Physics Objects (Rigidbodies) - Unity Official Tutorials
Description:

more » « less
Video Language:
English
Duration:
05:32

English subtitles

Revisions