Make things fall and collide
Tallow's physics uses Jolt. Two components do most of the work:
- A collider (Box, Sphere, Capsule, Mesh or Convex) gives an object a shape. On its own it makes the object fixed scenery.
- A Rigidbody makes an object move under gravity and collisions. It needs a collider on the same object.
Step 1: Make a floor
- GameObject > 3D Object > Cube, and rename it
Floor(F2). - Set its Scale to
20, 1, 20and its Position to0, -0.5, 0. - Add Component > Box Collider. The default size of
1, 1, 1fits the cube, and it is scaled along with it.
The floor has a collider but no Rigidbody, so it stays put.
Step 2: Drop a box
- Add another cube and move it up to
0, 5, 0. - Add a Box Collider, then a Rigidbody.
Press Play. The box falls and lands on the floor.
Step 3: Try the Rigidbody's settings
Stop, select the box and change some of these, then play again:
| Field | Effect |
|---|---|
| Mass | How heavy it is, in kilograms. Heavy things push light ones. |
| Bounciness | 0 lands dead. Near 1 bounces almost back to where it started. |
| Friction | How much it grips while sliding. |
| Gravity Scale | 0 floats. 2 falls twice as fast. |
| Is Kinematic | Moved by your scripts only, but still pushes other bodies. |
| Freeze Rotation | Never tips over. |
Turn the box a little (E, or Rotation 30, 0, 20) before playing to see it tumble.
Step 4: Add a ball and a pile
- GameObject > 3D Object > Sphere at
0.3, 8, 0, with a Sphere Collider and a Rigidbody. Set Bounciness to0.7. - Select the box and press Ctrl+D a few times, moving each copy up a little, to stack a tower.
Play. The ball drops onto the tower.
Step 5: Push things from a script
Create a script called Launcher (Project panel, Script > Basic Component) that throws its object upward when you press Space:
using System;
using Tallow;
namespace MyFirstGame;
[Component]
class Launcher : Script
{
public float strength = 8;
public override void Update(TickInput input, float deltaTime)
{
if (!input.Pressed(.Jump))
return;
let body = GetComponent<Rigidbody>();
if (body) // true when the object has a Rigidbody
body.AddImpulse(.(0, strength, 0));
}
}
In 0.2, engine components such as Rigidbody, Camera and CharacterController are handles: small values that point at the component's data. Check one with if (body), not body != null.
Add Launcher to the ball and play. Each press of Space throws it up. AddImpulse gives an instant kick. AddForce pushes steadily, every update you call it.
On 0.1: derive from
Component, usepublic override void Update(), checkInput.GetKeyDown(.Space), and test the body withbody != null.
Step 6: Where next
- Shape casts and overlaps to find what is in front of something or under it.
- Joints for hinges, springs and ropes.
- Destructible voxel objects and soft bodies.
- For anything that walks, use a Character Controller, covered in the next tutorial.