Gravity and Mass
Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 1 of 63.
A Rigidbody hands its object to Unity's physics, as the coconut showed when it fell. 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
EasyThe crane holds two crates over the dock: a heavy Safe and a light Box. Both have a Rigidbody and a CraneLoad script whose weight the Inspector sets (80 on the Safe, 2 on the Box).
- When the game starts, set the Rigidbody's mass to
weightand stop gravity pulling, so the crate hangs where it is. - When Space is pressed, let gravity pull it again, so it drops.
Press Play, press Space and watch which crate lands first, then Check.
Try it yourself
using UnityEngine;
public class CraneLoad : MonoBehaviour
{
public float weight = 1f;
void Start()
{
// Set the mass to weight and stop gravity pulling
}
void Update()
{
// Space: let gravity pull again
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Physics and Gameplay
1Rigidbody
Gravity and MassPush with AddForceJump with an ImpulseFixedUpdate for PhysicsVelocityKinematic BodiesRecap: Launch Pad2Collisions
OnCollisionEnterWhat Did I Hit?Break on ImpactStanding on the GroundBounce and FrictionCollider ShapesRecap: Breakable Wall