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
- 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.
- 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.
- Using the context sensitive gear menu, you can shift up or down the order of the components in the inspector pane.
- Under the Project Pane, create a new folder, Create → Folder and name it Scripts. Here we will keep our C# code files.
- Now, go to Assets menu → Create → C# Script or from Create menu under Project view pane.
- 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
)
- 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.
- The script will get loaded in Visual Studio, when we open the script by double clicking on it. Write the following C# code:
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);
}
}
- 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.
- Now, go back to Unity and run the program. To play your game, you have to click the Run button.
- 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.
- Go to GameObject menu → 3D object → Cube
- We will place the cubes around the sphere and the sphere will have to pass through all of them one by one. Like this:-
- 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.
- Place these cubes as you want or as shown in the above figure. You can add color also using the material.
- For tilting the cubes, you can select all the cubes and change their rotation transformation.