There may be function’s in the code example’s that I don’t explain very well / thoroughly, be sure to have the Unity documentation open at all times when you are developing, it’s great to just search out a function and find out what it does. They even have small code examples showing how to use them.
Goals
- Creating a remake of Pong
- Remake a simple AI to play against you.
- Adding a scoring and a high-score system.
Requirements
- Limited knowledge of C-Sharp
- A basic knowledge of Unity3D
Step 1 : Create the project
Create a project in Unity by starting up Unity and select the tab in the Project Wizard.
Step 2: Setting up folders
To add folders in Unity, find the project tab and select the “Create” drop-down and press Folder. This will create a folder with the name “New Folder”, change this to “Scripts”, repeat the process again for another folder called “Assets”.
Step 3: Creating the paddles
Creating the paddles in Unity can be done by selecting the drop-down bar in the top left called “Game Objects”, then select “Create Other” and then finally “Cube”.
This will place a cube object within the game. Select the cube and change the scale on the X axis to 5, this will make it look more like a paddle.
Step 4: Making the paddle into a “Prefab”
Click the Asset’s folder you created in Step 2, then press the “Create” drop-down again. Then select Prefab, this will create an item in the assets folder. Change the name of this item to “Paddle”, you can do this by clicking the item and hitting F2.
You will notice that this new item has an icon of a gray cube, to set-up the prefab with an object, select the paddle object in the hierarchy and drag it into the prefab item.
Step 5: Moving the paddle
To move the paddle you are going to need to write a script. To do this, select the scripts folder, select the “Create” drop-down and select “C Sharp Script”.
This will create a new script, rename this to PlayerPaddleController. Double click the newly created script and type the following into it.
using UnityEngine;
using System.Collections;
public class PlayerPaddleController : MonoBehaviour {
public int Speed = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 movement = new Vector3(Input.GetAxis("Horizontal") * Speed,0,0);
this.transform.Translate(movement * Time.deltaTime);
}
}
The above code is a very simple script that react’s to the user’s input on the horizontal axis (by default this is A and D or the arrow keys, as defined in the Edit > Project Settings > Input).
The line:
Vector3 movement = new Vector3(Input.GetAxis("Horizontal") * Speed,0,0);
Is used for setting the vector for the movement, as we want the paddle to move along the X axis only, we set the X variable to the input, and the rest to zero.
This vector is then passed to the next line:
this.transform.Translate(movement * Time.deltaTime);
Which translates (or moves) the object is applied to by the amount of movement specified in the movement vector. This is then multiplied by the Time.deltaTime number which makes the movement frame-independent meaning that if the frame-rate was to suddenly drop it would still move the same distance, it also allows you to set the speed as Units per second, than Units per frame.
Step 6: Attaching the script to the paddle
Attaching the script is as simple as dragging the script from the project folder and dropping it onto the prefab object. When you click the prefab you will see the script in the inspector along with the various other components that are attached to the object.
Here you can tweak public variables that are serializable and public (though it is possible to expose private variables, more on that later though!), in this example you can change the speed variable.
