A Brief Introduction To Physics In Unity
Physics are great, they’re a fundamental part to a lot of engineering, and game development is no different. With our game objects, we want to be able to adjust the physical properties. Unity has both a 2D and 3D built in physics engine. The two most basic elements of these respective systems are Colliders and Rigidbodies.
Colliders define the shape of our game objects, this is generally known as a hitbox. At it’s most simple, we can use static colliders to act as objects like walls. Having a collider with a Rigidbody attached would allow a player to hit the wall without the wall moving, whereas the player could have force applied. Colliders allow us to call special Unity functions around collisions to script further behaviors or enabling certain events. However, if we set the Is Trigger function on our collider to true, this instead allows colliders to pass through each other and with a Rigidbody attached, call Trigger functions in Unity.
Rigidbodies allow us to apply motion to our game objects using forces. For now we will stick with moving our players transform position, but there are more advanced cases where a physics-based character controller could better suit. With these forces, comes gravity, which luckily can easily be turned on and off. For our space shooter, we want our player to move freely without plummeting to the bottom of the screen.
Well if we aren’t using a physics based character controller, why do we need a Rigidbody? If one or more game objects have a collider attached and an impact occurs. The Rigidbody allows us to create the appropriate forces for that collision and move our game object accordingly.
Again, that’s not quite what we want to achieve in our current game. But if we have the is Trigger component of our collider set to active, we can enable the Rigidbody to call a function rather than a collision.
In our game, we can create an enemy. If that enemy collides with an instance of our laser, we want the enemy and laser to be destroyed. Having this simple physics system allows us to detect this collision and trigger a method in our code.
The method we want to use is private void OnTriggerEnter(Collider other)
Now when our enemy’s Rigidbody collider collides with another object, it will call the code we input. But we want to define what object does what action. Therefore we need to enable tags on our game objects. Having a player, laser, and enemy tag allows us to define these collisions using if statements.
Now when our laser hits our enemy, both game objects are destroyed. It’s also important what order our Destroy functions go in. Our laser needs to be destroyed first, because if we were to destroy the enemy first, the line of code calling for the laser to be destroyed would no longer exist.
Just to demonstrate the difference between collisions and triggers, lets explain what using OnCollisionEnter would do to our current mechanic. If none of our game objects were functioning as triggers. Our laser and enemy would create a collision, knocking each other off course. This could be of use if we wanted an asteroid feature in our game that could act as a physics object, and simply destroying the laser object in our OnCollisionEnter method, but for calling functions without physics being applied, OnTriggerEnter should be used.
If you are aware of how you want your objects to interact, with a bit of thought, the right choice can be made. If not, play around! Mess with the physics engine and find what feels the best.
Physics in Unity can be simulated to be as powerful as they are in real life. Thus there is plenty more we are able to control in our games. But knowing the use of Colliders and Rigidbodies are essential to creating some of the most basic functionality in your games.