Create A Rolling Ball Game With Force And Gravity In C#

Introduction

This article demonstrates how to create a rolling ball game with force and gravity using C# in Unity.

Prerequisites

Unity Environment version 5.6.1

Create Sphere

Step 1

First, you have to open the Unity project. Create terrain, trees, and water. Add skyboxes in your project. Create sphere into your game.

  

Right click on the Assets panel, select Create >> Material.

 

Material will be displayed in the Assets panel. Change the material color into yellow. Drag and drop the material into sphere.

 

Use gravity in your game

Step 2

Click on the sphere. Go to the Inspector window and click on "Add component". Select the "Physics" option.

 

Select RigidBody.

 

The rigid body will be added into the inspector panel. Tick on the "use gravity" checkbox to add Gravity in the sphere.

 

Go to Scripts. Right click on Scripts >> Create >> C# Scripts.

 

Rename the script as move. Drag and drop the move script into sphere. The default script will be displayed on the right side.

 

Go to the mono-development-Unity.

 

Write the code like the following.

  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. [RequireComponent (typeof(Rigidbody))]  
  6.   
  7. public class move : MonoBehaviour {  
  8.   
  9.     public float xForce = 10.0f;  
  10.     public float zForce = 10.0f;  
  11.     public float yForce = 500.0f;  
  12.   
  13.     //use this for initialization  
  14.   
  15.     void Start () {  
  16.           
  17.     }  
  18.     //Update is called once per frame  
  19.     void Update () {  
  20.         //this is for x axis' movement  
  21.   
  22.         float x = 0.0f;  
  23.         if (Input.GetKey (KeyCode.A)) {  
  24.             x = x - xForce;  
  25.         }  
  26.   
  27.         if (Input.GetKey (KeyCode.D)) {  
  28.             x = x + xForce;  
  29.         }  
  30.   
  31.         //this is for z axis' movement  
  32.   
  33.         float z = 0.0f;  
  34.         if (Input.GetKey (KeyCode.S)) {  
  35.             z = z - zForce;  
  36.         }  
  37.   
  38.         if (Input.GetKey (KeyCode.W)) {  
  39.             z = z + zForce;  
  40.         }  
  41.         //this is for z axis' movement  
  42.   
  43.         float y= 0.0f;  
  44.         if (Input.GetKeyDown (KeyCode.Space)) {  
  45.             y = yForce;  
  46.         }  
  47.   
  48.         GetComponent<Rigidbody> ().AddForce (x, y, z);  
  49.     }  
  50. }  

Save the program.

 

Come back to the Unity window. Click on the “Play” button. The sphere will be displayed.

 

Press the “W” key. The ball will be moved forward.

 

Press “A, S, D” respectively to move the ball in left, right, backward, and rolling the ball. Press the “Space bar” and the ball will jump.

 

Click on the GameObject and select 3D Object >>Cube.

 

Create Cube

Step 3

Increase the cube size using scale tool, like the following.

 

Set the main camera like below.

 

Rolling ball game with force and gravity

Step 4

Now, click on the “Play” button. The game view will be displayed.

 

You can play the game using “W, A, S, D” keys. The ball will be rolling in the cube.

 

Press the “space bar” and the ball will jump up.

 

Release the “space bar” to get the ball in the cube again.

 

Summary

I hope you understood how to create a rolling ball game with force and gravity using scripts in Unity.


Similar Articles