Introduction To Unity Physics – Bucky Ball

Introduction to Unity

Learning Unity basics with a simple Bucky Ball tutorial.

Unity is a powerful game engine that is responsible for some of the best indie games. With its amazing 2D & 3D physics, cross-platform support, and robust ecosystem, Unity makes it easy to develop interactive solutions and other experiences. 

In this tutorial, we will be creating a simple Bucky Ball project where a player-controlled ball has to fall in a bucket, to understand

the basics of 

  • GameObjects
  • Physics
  • Scripting
  • UI Canvas
  • Audio

The prerequisites for this course are a copy of Unity 2020.3 and Visual Studios on your machine.

Getting Started

To get started, download the starter project Download Material at the top and bottom of the tutorial.

Unzip the contents and open the BuckyBall project in Unity.

In the Unity Editor window, you’ll see the RW folder in project windows, which includes

  • Material: For materials 
  • Models: For 3D Models
  • Scene: The Bucky Ball scene
  • Music: Background music
  • Prefabs: Pre-Built components
  • Scripts: For Scripts
  • Textures: For Textures

Open the Bucky Ball scene from Scenes folders, and get ready.

Understanding Physics

Physics or Game Physics introduces real-world physics in simulations. All the physical activities in the real world like jumping, walking, etc can be calculated using discrete values, these calculations are coded in the physics engine. Component of physics engines like gravity can be used to add physics in video games. 

Unity’s Rigidbody component is used to add and modify physics like gravity, mass, angular drag, collision detection, etc. 

Implementing Input System 

To implement player movement in the horizontal and vertical axis, first, we need to add Rigidbody to our player. You can do that by selecting Sphere, which is present inside Player in the project hierarchy window. Once selected, you can click on Add Component in the inspector window, and select Rigidbody, present under Physics options.

Now, Open Player.cs from the Scripts folder and add the code given below.

public float speed = 200.0f;
   void FixedUpdate()
       float moveHorizontal = Input.GetAxis ("Horizontal");
       float moveVertical = Input.GetAxis ("Vertical");
       Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
       GetComponent<Rigidbody>().AddForce (movement * speed * Time.deltaTime);

Here, the Input.GetAxis() will detect the horizontal and vertical inputs, and displacement will be recorded as Vector3 movement. To make the object move, AddForce will be applied to the Rigidbody with parameters movement, speed, and deltaTime.

Now, select the Player game object located in the hierarchy tab and drag and drop the Player.cs script in the inspector tab.

Once added you will be able to control the ball with your arrow keys. To test, save and hit the Play button on the inspector.

Collision Detection

Game engines use collision engines to detect the collision of the geometry of the game objects. There are two types of collision detection, Continuous and Discrete. Continuous collision detection is more expensive as compared to discrete collision detection. Collision detection requires the Collider component to be added to the game object. 3D Colliders available in Unity are

  • Box Collider
  • Sphere Collider
  • Capsule Collider
  • Mesh Collider
  • Terrain Collider
  • Wheel Collider

By default, Colliders are already added to the primitive 3D models available in Unity. Imported 3D models require colliders to be added separately. 

Adding Colliders

To add colliders to the bucket, we need to open Bucket prefab from the Prefabs folder. Now, expand BucketModel present inside Bucket, and select all the children. Now, click on AddComponent, and add the Box Collider component from Physics

After adding Colliders to all the walls of the Bucket, add a script to detect whether the ball has fallen into the Bucket or rolled off after hitting the wall of the Bucket. To resolve this, we will be adding a script to detect collision on the bottom collider of the Bucket.  

To implement collision detection, open Bucket.cs located in the Scripts folder and add the code given below.

 void OnCollisionEnter(Collision collision)

Here, OnCollisionEnter(Collision collision) is a Unity event that detects the collision of objects with RigidBody. We have a Debug.log with colliders name for testing, which later will be replaced by the game complete screen activation.

Now, Save and Play. You will notice a debug log statement in the console panel.

Adding UI Panel

To add UI elements to the game, we will be using Unity’s Canvas. Canvas is the area where graphical user interface elements are overlaid. We have already placed a simple Level Complete UI panel inside the UI game object located under Canvas. Now, click on LevelCompleteText and type ‘Success!’ in the text component located in the Inspector tab.

Now, open the LevelUI script from the Scripts folder and paste the code given below.

public GameObject levelCompletePanel;
   public void LevelComplete()

Here, levelCompletePanel is the reference to the LevelCompleted UI panel, which we are activating by using levelCompletedPanel.SetActive(true) when LevelComplete() is called.

Now, attach the LevelUI script to the UI game object located inside Canvas. Once attached, drag and drop the LevelCompleted UI Panel on the reference of levelCompletePanel in the LevelUI script component.

Now, we need to call LevelComplete() from the LevelUI script when the collision is detected. Open Bucket script and replace the code with the one given below.

   public LevelUI levelUI;
   void OnCollisionEnter(Collision collision)

Here, the levelUI is a reference of the LevelUI script, from which we are calling levelUI.LevelComplete() method.

Now, drag and drop the UI game object located inside Canvas from the hierarchy and place it in the reference of LevelUI in the Bucket script component.

Once done, Save and Play

Adding Background Music

Unity uses the Audio Source component to playback audio files. To use Audio Sorce, we need an Audio Listener which is by default present in the Main Camera game object. 

To add background music, select the Audio Source game object located inside the Audio game object. Click on the AudioClip property in the Audio Source component and select TownTheme audio file.

Background music has been added to your game.

The Audio Source component has lots of features like looping, tempo, 3D audio property, etc, but for this tutorial, we will be playing only one audio file on loop.

Yay!, you have completed Bucky Ball, and now get ready to play. Before hitting Play, make sure to save the project.

Where to Go From Here?

You can download the starter and final project by clicking the Download Material button at the top and bottom.

Now that we have created the core mechanism of the game, you can add challenges to the game to make it more exciting. Try adding a countdown timer and moving the bucket to bring more fun to the game. You can also replace the Ramp with the more challenging one.

All the required assets are available in the project.

Thanks for reading this tutorial. :]

Hope you learned something new today. If you want to learn more, visit Learn Unity.

Thanks to Cetheil and Remaxim for CC-0 texture and music.

Written by

XR Developer responsible for end-to-end development of XR solutions spanning multiple domains, by using various XR and WebXR libraries.

Leave a Reply