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