Physics 101 — Simulating Physics in Unity
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.
If your object does not have a collider, one can be added in the Inspector!
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.
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!