Project 1: Space Shooter

Introduction to physics in Unity

With the help of our beloved enemies

It’s raining lasers, hallelujah! Wait, what…?

The lasers are locked and loaded, but there’s absolutely nothing to shoot at. To remedy this situation let’s add some enemies, for now a simple cube will do. For future purposes I make a Prefab out of it. To spice up my player’s life a bit, I add some randomness into my enemy’s script. If the enemy survives the trip all the way down the bottom, it will teleport back up to a random location along the x-axis.

The enemy script.

Now that I have something to collide with, preferably with my lasers though, I have to introduce some simple physics to my game. To do this, I have to enable Is Trigger in the Box Collider. This will ensure that when my player, or the laser, hits the enemy I can monitor this event and add some sort of behavior to it.

Enabling the Is Trigger in all and adding the RigidBody to the Laser and Enemy Prefabs.

Is this all? No, I also have to add a Rigidbody component to some of my objects. I choose to add it into my laser, because that has to hit the enemy and anything else I can come up with. I also add the Rigidbody component to my enemy. Of course, it could have been added to the player as well, but for now I think that the enemy and the laser are better options.

The Rigidbody component provides me with the necessary functionality to detect if my laser has hit the enemy or my enemy has hit me. It does provide even more, like for instance Gravity, which I have to remember to remove from my laser and enemy Prefabs. Unless I want to see them raining down once they come into play…

Disabling gravity from the RigidBody.

--

--

--

Unity game developer in the making.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Intro to Stacks in C++

Visual Studio Code 사용법.zip

No, 10x Developers Aren’t Legends Nor Do They Have Superpowers

The Launch of UCL API

How to Know if a Dedicated Development Team Is Right for You?

Kubernetes Pod Quality of Service (QoS) — DevOps Bytes

You Can’t Be a Programmer without Understanding Logic

Multipass and Microk8s on Windows

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Eero Saarinen

Eero Saarinen

Unity game developer in the making.

More from Medium

New type of fire powerup for the player

More Productive Editor Layout in Unity

Performance Tips for Unity — Other Optimizations #3

Collecting the Triple shot powerup