Unity Guide

Introduction to Physics in Unity

A quick guide about basic Physics in Unity

Objective: Start implementing the physics system of Unity for a prototype of a space shooter game.

In Unity, we can enable the physics system through the Rigidbody component. It allows the gameobjects to have physics properties that can be detected in conjunction with the collider components, which will represent the interactive bodies of the gameobjects.

It’s important to recognize different types of collisions to decide which one can apply in our game:

Hard surface collisions

This type of collisions are the ones we see everyday in the real world. When we kick a ball, when a coin falls and bounces off the solid floor, when a boxer hits an opponent and knocks it out and every collision that applies forces between objects.

Trigger collisions

These are collisions we usually see in games where a player needs to collect items (coins, power-up, etc.) and when it touches them they disappear from the scene. This type of collisions doesn’t need a physics force feedback when they happen.

Start physics system implementation

Following the space shooter prototype from older posts, we need to have a enemy with behavior to attack from above, just like we did with the player and the laser behavior, so I’m going to suppose that we already have the enemy like this in the scene:

Unity physics system is not enabled yet.

As you can see above, the enemy and the player can pass through each other because the physics system is not enabled yet.

We need to do the following steps to enable it in the gameobjects that will collide (player, enemy, laser):

  • Enable the Is Trigger option in the collider components to identify trigger collisions inside the scripts.
  • Add a rigidbody to the laser and the enemy. Then disable the Use Gravity option as we don’t want the gameobjects to fall in the scene.

Note: Do the first step for the player and both steps for the enemy and the laser prefab.

If you leave the gravity on, the enemy will fall from the scene like this:

This way, we’ll be able to detect trigger collisions from the scripts by using one of the functions that is already built in Unity’s monobehaviour help methods, but that’s a topic that I’ll explain in the next post.

And that’s it, you can add physic properties to your gameobjects in Unity! :D. I’ll see you in the next post, where I’ll be showing how to use the right method to detect collisions in your game.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).