Bouncing and sliding in 2D.
There are 4 items that work together
to make a bouncing ball in 2D.
The sprite image of the ball.
A collider component to give the image
a physical presence in the scene.
A physics material 2D assigned to the collider
to define the friction and bounciness.
And a rigidbody 2D component to allow the ball
to be affected by physics forces, including gravity,
and to participate in physical events,
like collisions and triggers.
To make a slippery ice-covered platform
the rigidbody 2D is not necessary
unless the platform is moving.
To create a bouncing ball first add
the sprite image to the scene.
In this form this is simply what the ball
will look like and it will not interact with any other
game objects in the scene.
To give this sprite image a physical presence
in the scene add a collider 2D component
to the game object.
One way this can be done is to use the
Add Component button on the game object.
And select Physics 2D
Circle Collider 2D.
The circle collider 2D is optimised for round objects.
For more information on colliders,
collider 2D and the circle collider 2D
please see the information linked below.
At this point the game object can participate
in physics events, but it cannot initiate them.
This game object will not react to physics
forces and is not influenced by gravity.
To do so we must add a rigidbody 2D
component to the game object.
This can be done by selecting
Add Component
Physics 2D
Rigidbody 2D.
Now the ball will fall under the influence of gravity.
But it is not very bouncy.
To make the ball bouncy use a
physics material 2D.
The physics material 2D is an asset
and it will be stored in the project view.
One way to create a new physics material 2D
is to select Create
Physics 2D Material.
This will create a new asset in our project view.
The new physics material 2D asset
will need editing.
First it needs to be named.
Bouncy.
There are 2 properties to a physics material 2D.
Friction and Bounciness.
The bouncy ball should have a small amount of friction.
But more importantly a large amount of bounciness.
One way to add the physics material 2D
to the collider component
is to simply drag and drop the physics material 2D
on to the Materials slot on the collider 2D component.
Now the ball not only reacts to physics forces
and the other colliders in the scene,
but it does so in a very bouncy manner.
To make a slippery ice-covered platform
we need only 3 of these items
unless the platform is moving.
First add a sprite image to the scene.
Then add a collider 2D component.
In this case the box collider 2D.
And then create and add a new physics material 2D
to the collider 2D component.
It is possible to edit the physics material
2D at any time,
even after adding it to the collider.
The ice-covered platform needs to be slippery
which means little or no friction.
Ice is also very hard and not very bouncy
so it should have little or no bounciness.
With this new slippery surface we can
see that items slip right off.
For more information on physics, physics 2D
and physics material 2D,
please see the information linked below.
If the platform were moving it would be best
to add a rigidbody 2D to the platform's game object.
And mark the rigidbody 2D as kinematic.
For more information on rigidbody,
rigidbody 2D and the Is Kinematic property
please see the information linked below.
Subtitles by the Amara.org community