Physics 101 — Simulating Physics in Unity

Jack Leavey
2 min readMay 22, 2022

--

Artist’s depiction of Unity calculating physics.

Bad news first; we have to discuss physics (and understand it). The good news it that Unity does most of the lifting for us, so long as we set up our system correctly.

Unity can calculate physics using several different systems to simulate everything from gravity to centripetal force. I’m going to stick to physics that apply to the 2D realm for now.

Unity looks for two things to calculate our simple physics; Rigidbodies and Colliders. The shape of the collider is generally irrelevant.

When creating geometry in Unity, it is automatically given a collider that matches it’s shape.

The bright green lines display the Box Collider associated with this cube.

If your object does not have a collider, one can be added in the Inspector!

Match the shape to the object you are using!

You can even have multiple colliders on an object for more complex simulation, but that is unnecessary for our goals.

The other piece needed is a Rigidbody. These can be added the same way as the colliders.

Make sure you use a 2D rigidbody when working in purely 2D projects.

A pickup system uses a soft collision system to give the appearance of collecting an object. We can use this simple framework to simulate destroying our enemy with the laser. Just think of the laser “collecting” the enemy.

To simulate soft collisions only one of the objects needs a rigidbody. In the current example of our Space Shooter, we have a player, a laser, and an enemy.

Because the enemy will collide with both the player and the laser in gameplay, I give the enemy prefab the rigidbody to save some resources.

On a game this small and simple, resource allocation is somewhat moot, but it is a good practice to establish early as you move onto greater and more demanding projects!

--

--

Jack Leavey
Jack Leavey

Written by Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!

No responses yet