Breakout: Physics

As a game engine, Unity provides a lot of functionality right out of the box. You could make some really fun physics based games and not need to have a math degree to do it! In fact, in this lesson we will start creating game objects that react to physics, with no programming required.

If you followed along with the first lesson, open your existing “Breakout” project. Otherwise, you can start from this sample project here.

Now open the “SampleScene” that was created along with the 2D template. From the File menu, choose “File -> Open Scene”, select the scene in the dialog, and click “Open”.

Creating Physics Objects

Right-click in the Hierarchy window, and choose “2D Object -> Physics -> Dynamic Sprite”.

Create a new dynamic physics object

Congratulations, you have just created a GameObject. A new entry appears in the Hierarchy window showing that you have added it to the Scene, and any selected GameObject will show additional information in the Inspector window. At the very top of the Inspector is a small section of properties about the GameObject itself, such as its name. Change the name of our GameObject to “Ball”.

Inspector to change the GameObject name

You can think of a GameObject as a collection of Components that really determines what it is and what it can do. The Components of a GameObject also appear in the Inspector window. Look at them now:

  • Transform – controls where in 3D space an object is located.
  • Sprite Renderer – is what makes the object show up in the camera. It determines what picture to show, called a Sprite. You can also change a variety of other properties like what “color” to tint it.
  • Circle Collider 2D – determines how to know when one object has hit another object.
  • Rigidbody 2D – causes an object to be controlled by Unity’s physics system.

Thanks to the configuration of Components provided already, our Ball will respond to physics forces. At the moment, the only “force” we have is gravity, so if you press Play, you will simply see your ball fall out of the view of the camera. Make sure to Stop the simulation before continuing.

Important:

It is possible to continue editing your scene while the simulation is playing. This includes modifying Component properties and even creating and destroying GameObjects. However any changes made while in play mode will be lost once exiting play mode.

To make things more interesting, let’s give the object something to collide with. Right-click in the Hierarchy window, and choose “2D Object -> Physics -> Static Sprite”. Change the name of our new game object to “Wall” using the Inspector window.

Move the Wall beneath the Ball. You can do this by left-click and dragging the object within the Scene window, or by manually adjusting the “Position” of its Transform component in the Inspector window.

Placed wall

If you press Play now, you will see the ball fall and then collide with the wall. If your Wall is rotated, the Ball would then roll along the edge before continuing to fall. Feel free to play with the positions, rotations, and scale of your objects to get a feel for it all, but make sure you Stop the simulation before continuing.

Did you wonder why the Wall does not also fall due to gravity? The answer is in the configuration of its Components. The Rigidbody 2D has a different “Body Type” value of “Static”. You may notice some other differences as well, such as that this object uses a Box Collider 2D instead of a Circle Collider 2D so that the collision shape better matches the Sprite image.

There is a third physics “Body Type” value of “Kinematic”. In general you could think of the types like so:

  • Static – a body that doesn’t move, but which can collide with other bodies. In our game, this is the walls that border the field.
  • Dynamic – a body that is moved by the physics system. In our game, this is the ball bouncing around the screen.
  • Kinematic – a body that is move by player input, and which can effect other bodies. In our game, this will be the paddle that the player moves back and forth.

Physics Materials

While seeing a ball move by gravity, and collide with other objects is already pretty neat, you may have been wondering why the ball looks so heavy? Why doesn’t it bounce? Unity allows you to control the physics “surface” of your objects. For example, you could make a rubber ball with high bounce, or a heavy bowling ball with low bounce.

Right-click the “Physics” folder in the Project window. In the pop-up menu, choose “Create -> 2D -> Physics Material 2D”.

Create a physics material

Name the new material “Ball”, then look in the Inspector window to adjust its values. Set “Friction” to 0, and set “Bounciness” to 1.

Set physics material values

Select the “Ball” GameObject in the Hierarchy window. Then look at the Rigidbody 2D Component in the Inspector window. Select the target icon for the “Material” field, and select the new Ball material.

Assign physics material

Press Play now, and you will see that the Ball will bounce up from the Wall with as much force as it had before the collision. In fact, it can continue bouncing like this forever. Had we used a “Bounciness” value less than 1, such as 0.5, then you would see the height of the bounce decrease with each collision. Feel free to play around with the values if you like, but set Bounciness back to 1 before continuing.

Save, Save, Save!

In this lesson we have modified the Scene by adding GameObjects. Note that anytime you have unsaved changes, your scene name will appear with an asterisk next to it.

From the file menu, choose “File -> Save” to save your changes.

Summary

In this lesson we learned about creating physics GameObjects. We reviewed what a GameObject is, and how it is ultimately given abilities through its Components. We created both Dynamic and Static Rigidbodies and used both Square and Circle Colliders. We discussed all three types of rigid bodies. We used a physics material to control the surface quality of our object such as how bouncy it is. Finally, we got to “Play” the Game and observe how Unity’s physics system was able to simulate gravity, collisions, and bouncing behaviors automatically.

If you’d like you can also download the completed project for this lesson here.

If you find value in my blog, you can support its continued development by becoming my patron. Visit my Patreon page here. Thanks!

2 thoughts on “Breakout: Physics

Leave a Reply

Your email address will not be published. Required fields are marked *