Introduction to Physics in Unity

Day 10- Making 2D Space Shooter Game

Unity has built in physics engines for 3D and 2D which allow you to simulate physics for gravity, collisions and other various forces by using the Rigidbody component.

Rigidbodies are components that allow a GameObject to react to real-time physics. This includes reactions to forces and gravity, mass, drag and momentum. You can attach a Rigidbody to your GameObject by simply clicking on Add Component and typing in Rigidbody in the search field.

With Rigidbody on your gameobject, It will immediately react to real-time physics like gravity and other collideable objects. You would also want to put some thought into which gameobject you want to put your Rigidbody on as adding Rigidbody to all gameobjects would hurt the performance of your game.

Adding Rigidbody is very easy; Just select on the gameobject you want to put it on, select Add component in inspector, search for Rigidbody and hit enter. You might want to select RigidBody2D if you are making a 2D game but note that 2D components won’t interact with 3D components.

Now enable your Is Trigger in your colliders, set your player tag to ‘Player’ and write the above code which detects the collision between enemy and player and damage the player health while destroying the enemy gameobject.

As you can see when the enemy which is red collides with player which is blue, the enemy gameobject is destroyed.

We will talk more about Collision triggers on next article.

--

--

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