Introduction to Physics in Unity!

addam davis
3 min readMay 11, 2021

Today’s lesson sounds intimidatingly but it’s simpler than you would think. physics ensures that the objects correctly accelerate, respond to collision, gravity, and other forces. Unity provides different physics engines for you to use depending on your needs.

For today, we are going to focus on using physics to detect collision. In Unity there are two different types of collision. Hard surface collision and Trigger collisions.

Imagine you threw a ball at a wall, they collide, and the ball comes back to you, that’s hard surface.

Now, imagine you’re playing an indie title where you some plumber and you collect coins. That’s a trigger collision.

We are going to use trigger collision today. To start this lesson, you need to make a cube and Label it Enemy. Give it its own script and his motion down so it will collide with the player at a speed of 4.

This should put the enemy directly above your player and on a collision course. Time to add Rigidbody to the Laser, and the Enemy. Select object and on the bottom of the inspector view you can add component. Click add component and type Rigidbody. Make sure once you’ve added the Rigidbody you uncheck the box that says add gravity.

Open the Enemy script and we are going to add a method.

There are other on trigger methods, but we will discuss using those other methods next time, for today we will need this method.

Under this method we will type the command:

In this line (other) is the object, (transform) is the root of the object, and (name) is the name of the object.

Now if we start the game and we pass through the enemy with the player the console will announce that the player collided with the Enemy, or if we shoot it with our laser then the console will say Laser.

Once the enemy is passed, he will fall forever, or until you restart the program. Alternatively, we could use some of our past knowledge and teleport the enemy back to the top of the game. We need to us an if statement.

If we wanted to spice it up and make it more of a game, with a randomized respawn we could use Random.Range. With this we could set a minimum and maximum value and it would generate a number between the two. Since this is a 2d game we only need to worry about the x axis.

That will do it. This is the basics of physics with Unity. Next, we’ll go over the different methods I mentioned earlier. Enjoy your coding adventures, and don’t be afraid to experiment with your codes!