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