Friday, September 19, 2014

UNITY-3D BEGINNER GAME TUTORIAL

by Swetha Naidu, Logan Sales, and Nirav Sharda

Introduction:

The main objective of the game is to make the ball go through the hole. There are walls which act as obstruction for the ball. There is a timer which counts the time taken by the ball to pass the hole. There is also a penalty of five seconds if the ball touches any of the red walls. The screen shot for the game play is shown in the figure below.



Figure 1: Game Play with the ball at the starting position.


The player tries to pass the ball through the hole. If the ball successfully passes the hole a text message “YOU WIN” is displayed and the timer stops. The time taken by the ball to pass through the hole from the beginning is displayed. The objective is to finish the game as early as possible with the minimum possible penalty.



Figure 2: Game Play when the player wins.

Controls for the game:
The controls for the game are arrow keys. Arrow keys rotate the plane as a result of which the ball moves. The arrow keys rotate the plane in two dimensions i.e. about the horizontal and vertical axes. The speed of the ball is according to the tilt of the plane.

Creation of Scene in Unity:
For the purpose of creating the game firstly a new scene needs to be created. The following unity game objects are present in the scene.
  1. Plane - A plane is like the ground.
  2. Walls - Walls are present on the Plane which have penalties.
  3. Ball - Ball which moves based on rotation of the plane.

Coding the game:

There are several steps involved in the creation of the game.

1.)  Rotation of the Plane:

First of all, you will want an empty game object to hold the main plane of the game as well as the walls and whatever else you want to place in the game that will rotate on input. From there, the plane and walls will be parented by that object.

Figure 3: Code snippet for rotating the platform.

In the update function we rotate the platform using the user input. The Input.GetAxis method will return values from -1 to 1 which is perfect for positive and negative rotation around the x and z axis. We all so don’t want the platform to rotate too much in any direction, otherwise the player will fall off, so we want to clamp it’s rotation.

Figure 4: Code snippet for clamping rotation.


This function does just that. Since we only want rotations from -10 to 10 (or 350 to 10) we can’t just use the Mathf.Clamp function so we wrote our own. this will return either the maximum, minimum, or initial rotation value.

2) Ball movement based on the rotation of the plane:
Figure 5: Code snippet for ball movement.

The rigidbody.addforce method adds force to the ball object. The force is added based on the rotation of the plane. The addforce methods need a vector3 input. Time.deltaTime is used to perform something by time and not by frame. By default everything is done per frame.

3) Displaying the time:
Figure 6: Code snippet for displaying time

A Boolean variable isGameSuccessful is set to true when the game ends. If the Boolean is false the totalTime variable is incremented by one. And when the game ends the variable becomes true and time is displayed in a GUI text component in unity3d. The update method in unity is called once per frame.

4) Displaying You WIN text:
Figure 7: Code snippet for displaying text.

The transform.position.x gives the x coordinate of the player i.e. ball and similarly we can get the y and z coordinates. When the ball reaches the opening hole, there are co-ordinates associated with that hole. Just check for those co-ordinates in the loop and set the text when the ball exits the opening and also set the Boolean isGameSuccessful true which is used for displaying the time.

5) Adding penalty if player(ball) hits the walls:
Figure 8: Code snippet for adding penalty to totaltime.

This should look fairly straightforward as it is a similar technique we saw with the pick-ups in the official tutorial. When the player collides with a gameobject tagged as “Wall” we increment the total time the player is taking to finish the game by an amount we can set in the Unity editor. For now, we chose that value to be 5, as it is a nice round number to use for a penalty.Now, in order for this code to work, we created a prefab “Wall” object that was nothing more than a cube with a child rigid body with “isTrigger” set. This way, the player can still collide with the walls and set off the trigger for this code.

No comments:

Post a Comment