Gravity and Mass
Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 1 of 71.
A Rigidbody hands its object to Unity's physics, as it did when you added one in Add a Component. From then on gravity speeds the object up by 9.81 m/s every second, and the Rigidbody has the settings that decide how. Two of them are mass, in kilograms (1 for a new Rigidbody), and useGravity (on for a new Rigidbody).
Mass does not change how fast things fall. Gravity pulls a heavy body harder, and the heavy body is harder to speed up by exactly as much, so a 1 kg crate and a 50 kg anvil dropped from the same height land on the same frame. Mass matters when bodies push each other: the anvil knocks the crate aside, and the crate hardly moves the anvil. This party balloon gets a light mass and is left alone by gravity, so it hangs where the scene put it:
void Start()
{
Rigidbody rb = GetComponent<Rigidbody>();
rb.mass = 0.2f;
rb.useGravity = false;
}With useGravity off, the balloon still has its mass and still moves when something bumps into it: only gravity stops pulling. Set it back to true and gravity pulls again from wherever the object is. There is no air in Unity's physics either, so a feather falls like a brick unless you give its Rigidbody some Linear Damping.
Challenge
EasyHang the two crates, then drop them together on Space.
- In
Start(), below theGetComponentline, setrb.mass = weight;andrb.useGravity = false;. - In
Update(), onInput.GetKeyDown(KeyCode.Space), setrb.useGravity = true;.
Press Play and press Space: the heavy Safe and the light Box land together. Then Check.
Try it yourself
using UnityEngine;
public class CraneLoad : MonoBehaviour
{
public float weight = 1f;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
// Set the mass to weight and stop gravity pulling
}
void Update()
{
// Space: let gravity pull again
}
}
All lessons in Physics and Gameplay
1Rigidbody
Gravity and MassPush with AddForceJump with an ImpulseFixedUpdate for PhysicsVelocityKinematic BodiesRecap: Launch Pad4Coroutines
InvokeInvokeRepeating, CancelInvokeYour First CoroutineLoops in CoroutinesStopping CoroutinesRecap: Countdown10Project: Arena Escape
The ArenaHunters2Collisions
OnCollisionEnterWhat Did I Hit?Break on ImpactStanding on the GroundBounce and FrictionCollider ShapesRecap: Breakable WallPractice on your own: Unity playground