RigidBody
Intro
A Rigidbody in Unity is a component that enables a GameObject to react to physics, like gravity and collisions. It allows movement through forces making the object behave more realistically in the game world.
Here are some typical configurations using a RigidBody:
Indication
Blocking + Moving
Object moves and responds to gravity or impulses, can collide and bounce with other elements
Useful for: moving car, falling domino, basket-ball
Set: Collider + RigidBody
Function: void OnCollisionEnter(Collision collision)
Blocking + Immobile
Object doesn’t move, but other objects can bump into it
Useful for: walls, floor
Set: Collider only
Function: None
Avertissement
Beware, when the trigger of the collider is activated, this implicitly disables the rigidbody of the gameobject.
Avertissement
In previous project, we have moved objects using the translate or rotate function.
Exercise
Basic setup
We begin our study by adding a RigidBody to the sphere.
Create a cube and and assign it a color
Scale the cube to create a ground
Select the sphere
If you want the camera to shoot the same view as the scene view
In the Hierarchy, select the camera
Right click and select Align With View
Enter Play mode
What happened?
The sphere has a Rigidbody, moreover it is affected by gravity so it falls
The ground don’t have a Rigidbody, it’s a static object, it cant move
Both the sphere and the ground have a Collider, allowing them to come into contact, which stops the sphere’s fall
Simulation
We add some new elements to the scene; try to reproduce the scene below.
Some hints: