4. Physics

So far the level is solid and the player walks on it. In this part you add things that move by themselves: crates that fall, stack and get pushed.

Three kinds of body

Anything with a collider is a physics body. A Rigidbody component decides which kind.

KindMoves howUse it for
StaticNeverGround, walls. This is what a collider with no Rigidbody is
DynamicGravity, forces and collisions move itCrates, balls, anything that should tumble
KinematicIts entity's transform moves it, and it pushes dynamic bodiesLifts, doors, moving platforms

The ground and the steps are static already. The player is a Character Controller, which is a kind of its own.

Add a crate

  1. Press + and Create Cube. Name it Crate 1, and set its Position to -3, 0.5, 2.
  2. Add a Box Collider.
  3. Add a Rigidbody. Set Type to Dynamic, Mass to 8 and Friction to 0.6.
  4. Make a brown material called Wood and put it on the crate.

Duplicate it twice with Ctrl+D and place the copies:

NamePositionRotation
Crate 2-1.6, 0.5, 2.30, 20, 0
Crate 3-2.3, 1.5, 2.150, 45, 0

The third sits on top of the other two.

Press Play and walk into the stack. The crates slide and topple. Stop, and they are back where you put them: stopping the game restores the scene exactly as it was when you pressed Play.

See what physics sees

Press Colliders in the viewport's toolbar. Every collider is outlined: green for static bodies, orange for dynamic ones, cyan for the character.

The level with collider outlines on and a crate selected

When something falls through the floor or floats above it, look here first. A box that is drawn but has no outline has no collider.

Tune it

Select a crate and try the Rigidbody's values. Press Play after each change.

  • Mass is in kilograms. The character has a mass too (70 by default), and a heavier crate is harder to push. Try 80.
  • Friction is how much it grips: 0 is ice, 1 is rubber on concrete.
  • Restitution is bounce: 0 lands dead, 1 bounces back to where it fell from. Lift a crate to height 5 and try 0.8.

Push from a script

You do not need a script to push crates, the character does that itself. A script can still move bodies when you want it to. This one kicks a crate upwards when the player touches it:

Lua
TypeScript
local Bouncy = {}

function Bouncy:OnCollisionEnter(other)
	if other.Tag == "Player" then
		self.Entity.Rigidbody:AddImpulse(Vec3(0, 40, 0))
	end
end

return Bouncy

OnCollisionEnter is called on a script when its entity's body starts touching another. other is the other entity. An impulse is one sudden shove, and AddForce is a steady push for use in OnFixedUpdate.

This script is only an example. The game does not need it.

What you learned

  • A collider gives an entity a shape, and a Rigidbody decides how it moves.
  • Stopping the game puts the scene back.
  • The Colliders button shows the physics shapes.
  • Scripts hear about contacts through OnCollisionEnter.

More in Physics and Rigid bodies and joints.

Next: coins and a score.