Signup/Sign In

Game Component's Movement and Play in Unity - Part 2

In the last tutorial we have already seen how to create the game components and objects and adjust their position, color and size, which was like laying foundation for our maze game. Also we have worked on adding a new material which helps in adding color to the objects.

In this tutorial we will learn how to move the ball and add cubes as obstacles and let the ball pass through them.


Stepwise Development of Maze Ball Game

So the idea is to create a maze with a ball which has to cross the maze. Sounds simple? Well then let's continue.

Making the Ball move

  1. To move the ball, you have to use the concepts of physics. So, the game object (ball) must have the RigidBody component attached to it.
  2. So to apply this, select the game object (sphere) and then go to Component menu → Physics → RigidBody. This will attache the RigidBody property to the selected game component.
  3. Using the context sensitive gear menu, you can shift up or down the order of the components in the inspector pane.

    Game Component Development


  4. Under the Project Pane, create a new folder, Create → Folder and name it Scripts. Here we will keep our C# code files.
  5. Now, go to Assets menu → Create → C# Script or from Create menu under Project view pane.
  6. Otherwise, you can also directly select the player (sphere) Game object, and then click the Add Component button under the inspector Pane. Add Component → New Script → name your C# script... (for example: ball_move)
  7. Upon doing this, Unity will create and attach the script to our selected game object. Now double click on the script asset.

    Note: Visual Studio must be installed in your computer in order to write C# code.

  8. The script will get loaded in Visual Studio, when we open the script by double clicking on it. Write the following C# code:
  9. 
    using UnityEngine;
    using System.Collections;
    
    public class ball_move : MonoBehaviour {
    
        public float speed;
        private Rigidbody rigb;
        void Start()
        {
            rigb = GetComponent<Rigidbody>();
    
        }
        void FixedUpdate()
        {
            float moveHorz = Input.GetAxis("Horizontal");
            float moveVert = Input.GetAxis("Vertical");
     
            Vector3 movement = new Vector3(moveHoriz, 0.0f, moveVert);
            rigb.AddForce(movement * speed);
        }
    }
    
  10. Here Vector3 is used to add a force to your rigidbody component (sphere) from all (x, y, z) axis directions. Compile the program, to check for errors.
  11. Now, go back to Unity and run the program. To play your game, you have to click the Run button.

    Game Component Development


  12. Now use keyboard's cursor keys to move the ball.

Adding Cubes as obstacles

Now it's time to add obstacles to our game, using the 3D cubes.

  1. Go to GameObject menu → 3D object → Cube
  2. Game Component Development


  3. We will place the cubes around the sphere and the sphere will have to pass through all of them one by one. Like this:-

    Game Component Development


  4. To add more than one cube to the scene, you don't have to repeat the step 1. There's a shortcut for this. You can either go to Edit menu → Duplicate option, after selecting the game object you want a copy of (here, cube) or you can select the cube and press the shortcut key Ctrl + D.

    Game Component Development


  5. Place these cubes as you want or as shown in the above figure. You can add color also using the material.
  6. For tilting the cubes, you can select all the cubes and change their rotation transformation.