Adventures in GameDev with GameDevHQ! Day 13, an introduction to Physics in Unity.

Esteban Ibarra
3 min readMar 25, 2021

In the past, if you wanted to write a videogame and have your sprites or objects behave realistically to realworld physics rules, you would have to understand a lot of higher math like trigonometry, calculus, and yes…physics, and you would do all of the algorithmic heavy lifting yourself. Thankfully those days are over and you can rely on Unity to do all of the physics for you so you can rely on just the game design logic!

Unity has a couple built in physics engines depending on whether you’re doing a 2D or 3D game, let’s say you’re making the next cool 2D Platformer that relies on physics, you’ll be using the Box2D physics engine. If you’re coding a 3D game, then you’ll be using NVideas PhysX engine. There are more engines available through Unitys packaging system like HAVOK and DOTS as well as more 3rd party extensions available in the Unity asset store that focus on a variety of specific improvements like bones, water, pinball physics or vehicles.

For our purposes, we’ll rely on Unitys built in physics as it’s already a pretty powerful tool. So what does physics in unity do? It allows GameObjects to be controlled by virtual realworld forces like gravity, velocity, acceleration, etc.

Physics work by a combination of Colliders and Rigidbodies.

Colliders tell Unity when..Things Collide! There’s also two types of collisions. Hard Collisions and Trigger Collisions.

Hard Collisions are when two physical objects ram into each other like 2 cars on a race-track slam into one another or when a player runs into a wall. Trigger collisions are things like when a player collects a coin, the coin isn’t going to stop the player cold, the player is just going to keep on running and collecting the coin won’t slow him down one bit!

Colliders won’t really work until the Physics engine is activated and this is done by adding a Rigidbody (or Rigidbody2D) component to your GameObject. Rigidbody, which will introduce properties like gravity, mass, drag, etc. into your object and they will work automatically without you needing to do anything!

While we can access our Rigidbody via a command called GetComponent, this is an advanced topic which we will get to later, but for now we can also access our colliders much more simply with OnCollisionEnter and OnTriggerEnter. Did you recognize the 2 types of collision? You can easily select which one you need via a checkbox telling Unity whether the collider is a trigger or not.

That’s it for now! Tomorrow we’ll take a deeper dive into scripting the colliders with our shootemup game!

--

--