< Return to Video

Rigidbody 2D - Official Unity Tutorial

  • 0:01 - 0:04
    The rigidbody 2D is a component that allows
  • 0:04 - 0:07
    a game object to be affected by 2D physics.
  • 0:08 - 0:11
    The game object must also have a collider 2D
  • 0:11 - 0:14
    component attached to interact with other game objects
  • 0:14 - 0:15
    using 2D physics.
  • 0:16 - 0:19
    A rigidbody 2D is required when working with
  • 0:19 - 0:21
    specialised 2D physics components
  • 0:21 - 0:23
    such as 2D joints.
  • 0:24 - 0:27
    The rigidbody 2D is also used to define important
  • 0:27 - 0:29
    physical properties about the game object,
  • 0:29 - 0:32
    such as it's mass, linear and angular drag,
  • 0:32 - 0:35
    gravity scale and other properties.
  • 0:36 - 0:38
    It is worth noting that unlike 3D physics
  • 0:38 - 0:41
    the rigidbody 2D has a gravity scale
  • 0:41 - 0:43
    which allows gravity to be set on a
  • 0:43 - 0:45
    per-game-object basis.
  • 0:46 - 0:48
    In this scene the prop_ammo_crate
  • 0:48 - 0:51
    is a simple game object with a sprite
  • 0:51 - 0:53
    renderer to hold and render the sprite.
  • 0:55 - 0:58
    Enter play mode, and nothing happens.
  • 1:00 - 1:03
    2D physics behaviour can be added to the ammo crate
  • 1:03 - 1:05
    by attaching a rigidbody 2D
  • 1:05 - 1:12
    with Add Component - Physics 2D - Rigidbody 2D.
  • 1:14 - 1:16
    It is worth noting that there are other ways to
  • 1:16 - 1:18
    attach a component to a game object.
  • 1:20 - 1:22
    For more information on components and
  • 1:22 - 1:23
    how to add them to game objects
  • 1:23 - 1:25
    please see the information linked below.
  • 1:27 - 1:30
    When in play mode, the ammo crate now falls
  • 1:30 - 1:32
    under the influence of gravity.
  • 1:32 - 1:34
    But it does not collide with the space ship.
  • 1:36 - 1:39
    To interact with other game objects using 2D physics
  • 1:39 - 1:43
    all game objects involved need collider 2d components
  • 1:43 - 1:47
    and at least 1 needs to have a rigidbody 2D attached.
  • 1:48 - 1:51
    The space ship already has a 2D collider.
  • 1:51 - 1:53
    A collider 2D component can be added
  • 1:53 - 1:55
    to the ammo crate by selecting
  • 1:55 - 2:01
    Add Component - Physics 2D - Box Collider.
  • 2:03 - 2:05
    The size of the 2D collider will automatically
  • 2:05 - 2:07
    match the size of the sprite.
  • 2:08 - 2:10
    In this case the collider needs to be adjusted
  • 2:10 - 2:13
    to match the image of the ammo box.
  • 2:17 - 2:19
    Now, when in play mode,
  • 2:20 - 2:22
    the ammo crate collides with, and comes to rest on
  • 2:22 - 2:24
    the top of the space ship.
  • 2:25 - 2:27
    For more information on 2D colliders
  • 2:27 - 2:29
    please see the information linked below.
  • 2:31 - 2:34
    The rigidbody 2D component not only allows a
  • 2:34 - 2:36
    game object to be affected by 2D physics,
  • 2:36 - 2:39
    it is used to define important physical
  • 2:39 - 2:41
    properties about that game object.
  • 2:43 - 2:46
    Mass is the mass of the rigidbody 2D.
  • 2:46 - 2:48
    2D rigidbodies with higher mass,
  • 2:48 - 2:50
    like those in the real world,
  • 2:50 - 2:52
    take more force to move and will have greater affect
  • 2:52 - 2:55
    when colliding with other 2D rigidbodies.
  • 2:56 - 2:58
    In this first case the ammo crate
  • 2:58 - 3:00
    and the asteroid have the same mass.
  • 3:06 - 3:08
    When the mass is changed on the asteroid
  • 3:09 - 3:11
    the interaction between the two game objects
  • 3:11 - 3:13
    is significantly different.
  • 3:19 - 3:21
    Linear and Angular drag affect
  • 3:21 - 3:24
    the velocity of 2D rigidbodies.
  • 3:25 - 3:28
    Think of this as something similar to air resistance.
  • 3:29 - 3:31
    Linear drag will slow the speed, or velocity
  • 3:31 - 3:33
    of the rigidbody 2D.
  • 3:40 - 3:42
    And angular drag will slow it's rotation
  • 3:42 - 3:44
    or angular velocity.
  • 4:01 - 4:05
    Gravity Scale controls how gravity affects the rigidbody 2D.
  • 4:06 - 4:08
    Unlike changing the global value of gravity
  • 4:08 - 4:10
    gravity scale allows for precise
  • 4:10 - 4:13
    control on a per-game-object basis.
  • 4:14 - 4:16
    When in play mode the ammo crate
  • 4:16 - 4:18
    falls under the influence of gravity.
  • 4:19 - 4:21
    Lower the gravity scale value
  • 4:24 - 4:26
    and the game object falls more slowly.
  • 4:27 - 4:29
    To disable gravity set this value to 0.
  • 4:31 - 4:33
    When Fixed Angle is selected the rigidbody
  • 4:33 - 4:36
    2D will respond to 2D physics forces
  • 4:36 - 4:38
    but it will not rotate.
  • 4:38 - 4:41
    Under normal conditions the asteroid will fall,
  • 4:41 - 4:43
    collide with the ammo box and roll away.
  • 4:45 - 4:47
    When Fixed Angle is selected
  • 4:47 - 4:51
    the asteroid will continue to react to physical forces,
  • 4:52 - 4:53
    such as the collision.
  • 4:54 - 4:56
    But it will not rotate.
  • 4:58 - 5:00
    When Is Kinematic is selected
  • 5:00 - 5:03
    the rigidbody 2D is considered to be a
  • 5:03 - 5:05
    kinematic rigidbody 2D
  • 5:05 - 5:08
    and is not affected by 2D physics forces,
  • 5:08 - 5:10
    including gravity and collisions.
  • 5:11 - 5:13
    This is typically done when moving a game object
  • 5:13 - 5:15
    with a 2D collider without using
  • 5:15 - 5:17
    physics or physics forces.
  • 5:17 - 5:20
    For example, when creating a moving platform.
  • 5:20 - 5:22
    The platform could be controlled by a
  • 5:22 - 5:24
    script or animation
  • 5:24 - 5:26
    and be moved by it's transform,
  • 5:26 - 5:28
    not by physics forces.
  • 5:29 - 5:31
    However, the platform needs to interact
  • 5:31 - 5:33
    with other 2D game objects.
  • 5:33 - 5:35
    So the platform needs a 2D collider
  • 5:35 - 5:37
    attached as well.
  • 5:38 - 5:40
    This means there is now a moving game object
  • 5:40 - 5:42
    with a collider 2D attached.
  • 5:44 - 5:47
    When moving a collider 2D while the game is playing
  • 5:47 - 5:50
    it is highly recommended that a rigidbody 2D
  • 5:50 - 5:51
    be attached as well.
  • 5:52 - 5:54
    Yet we don't want this platform to react
  • 5:54 - 5:56
    to physics forces.
  • 5:57 - 5:59
    In this case, the rigidbody will be set
  • 5:59 - 6:01
    to Is Kinematic.
  • 6:02 - 6:04
    This platform is moved via scripting
  • 6:04 - 6:06
    with a 2D collider attached.
  • 6:17 - 6:18
    The Interpolate settings can help smooth
  • 6:18 - 6:20
    movement of a game object.
  • 6:20 - 6:22
    If a game object appears to jitter when
  • 6:22 - 6:25
    being moved by it's rigidbody 2D
  • 6:25 - 6:27
    use the Interpolate settings to help
  • 6:27 - 6:29
    smooth it's transform movement.
  • 6:30 - 6:32
    The Interpolate setting will smooth the
  • 6:32 - 6:34
    movement based on the game object's
  • 6:34 - 6:36
    position in the previous frame.
  • 6:37 - 6:39
    And the Extrapolate setting will use the
  • 6:39 - 6:41
    predicted position in the next frame.
  • 6:43 - 6:45
    Sleeping Mode controls how the rigidbody
  • 6:45 - 6:47
    will sleep to save processing time.
  • 6:48 - 6:50
    Never Sleep will disable sleeping
  • 6:51 - 6:53
    Start Awake will make sure the rigidbody 2D
  • 6:53 - 6:56
    is awake when instantiated.
  • 6:56 - 6:58
    Start Asleep will initially set the
  • 6:58 - 7:00
    rigidbody 2D to sleep
  • 7:00 - 7:03
    but it can be woken by collisions.
  • 7:04 - 7:06
    For more information on sleeping
  • 7:06 - 7:08
    please see the information linked below.
  • 7:10 - 7:12
    The collision detection settings control
  • 7:12 - 7:14
    the type of collision detection used
  • 7:14 - 7:16
    when a rigidbody 2D.
  • 7:17 - 7:19
    The default is Discrete
  • 7:19 - 7:21
    and it is best to use discrete unless
  • 7:21 - 7:23
    there are problems.
  • 7:23 - 7:25
    In this case collisions are registered if
  • 7:25 - 7:27
    the game object's 2D collider
  • 7:27 - 7:31
    is in contact with another during a physics update.
  • 7:32 - 7:34
    Continuous is for fast moving game objects
  • 7:34 - 7:36
    and collisions are registered if the game object
  • 7:36 - 7:40
    appears to have collided with another between updates.
  • 7:42 - 7:46
    For more information on physics 2D and the rigidbody 2D
  • 7:46 - 7:49
    please see the information linked below.
Title:
Rigidbody 2D - Official Unity Tutorial
Description:

more » « less
Video Language:
English
Duration:
07:50

Spanish subtitles

Revisions