Using Physics in Unity

Christopher Pearl
4 min readJul 6, 2021

--

In order for my space shooter game to be really challenging to the user, I wanted to give the enemy ships in my space shooter the ability to move as if they were attacking the main player. Also, if the enemy and space shooter collide, I wanted the game to react and destroy both of the ships accordingly. In order to give my enemy ships behavior, I needed to use physics within the Unity editor.

OnCollisionEnter vs. OnTriggerEnter

Colliders are used to in Unity to detect collisions of 2 or more objects as if they were in the real world. There are 2 types of collisions: hard surface collisions and trigger collisions. Trigger collisions are like collecting a coin in Mario Kart. There isn’t a physics force that acts on you. Hard Surface collisions are similar to throwing a ball off a wall. For my space shooter, I want the laser to pass through the enemy ship and create the illusion that it is destroyed. To do this, I added a box collider to my player(space shooter), enemy ship, and laser prefabs. To enable them to pass through the object and have an event occur(collecting the enemy after destroying it), I checked the Is Trigger box.

Rigidbody Physics in Unity

I added a Rigidbody to only one of my gameobjects. Adding too many rigidbodies can be performance intensive, so I attached it to my enemy ship prefab. When I was looking for what method to use to cause the rigidbody to act on the gameobjects, I had two options: OnCollison and OnTrigger. I used the OnTrigger method in my Enemy script. If the player hits the box collider of on the Enemy ship, it will damage the player (space shooter).

I also created a lives variable in the player script. The player will start off with 3 lives and the decrement every time the it collides with an enemy ship! Though I set the variable to private, this can be increased or decreased at anytime in Unity with the [SerializedField] attribute attached.

Using GetComponent<> and Tags in Unity

Using Getcomponent<> allowed me to grab the component I wanted (Player) and create game logic to trigger the collider attached to the Player and damage the player if it collides with an enemy ship until it is destroyed. I created a player variable to hold the Player object’s GetComponent attribute. If the player component is pointed to, the player will be given damage and lose a life (the collider was hit by an enemy ship’s collider).

Tags are used in Unity to label the Gameobjects inside the Inspector. Something that can also be useful is using the tags in conditions and referring to components in my code. I used them in my enemy script, referring back to the player and laser gameobjects in unity. If the Laser tag is triggered, the laser will be destroyed(this.gameobject) and the object(other.gameobject) that it touched (collider) will be destroyed as well.

Now, when my laser is shot from space shooter and collides with my enemy ships, the enemy ships will be destroyed and disappear out of view of the game!

--

--

Christopher Pearl

Unity Developer, AR/VR Developer, Virtual Reality Product Manager for Micro Medical Devices Inc.