Cubes!
Plan:
-Create a sphere which moves horizontally around the game scene.
-Drop cubes down from the top, and if a cube touches the sphere, the game is over.
-Create a sphere which moves horizontally around the game scene.
-Drop cubes down from the top, and if a cube touches the sphere, the game is over.
Create a new project, as always.
We could use that background, but I think it would be better if it was all one colour. Select the Main Camera, and in the "Camera" component in the Inspector, change the "Clear Flags" option to "Solid Color".
The scene should now look something like this:
I will use solid blue as my background, but you can change it if you want to. Under "Clear Flags" in Main Camera's Inspector, there is a "Background" option. Click on the block of blue next to it, and change it to whatever colour you wish.
Now, we need three folders: our typical "_Scenes" and "Scripts", and a new one, "Prefabs". A Prefab is an example GameObject that Unity can use when instantiating (creating in the world) things. As always, save the scene into the "_Scenes" folder by pressing Ctrl+S or Command+S.
Now, we need three folders: our typical "_Scenes" and "Scripts", and a new one, "Prefabs". A Prefab is an example GameObject that Unity can use when instantiating (creating in the world) things. As always, save the scene into the "_Scenes" folder by pressing Ctrl+S or Command+S.
Enough setup! Let's get into the actual game here. We now need a cube and a sphere in the scene. Leave the cube where it is, but move the sphere to (0, -3, 0) in the Position part of the Transform. It should look something like this:
Add a Rigidbody to the cube by entering its Inspector and clicking "Add Component" typing "Rigidbody", and pressing Enter. Now, open the Prefabs folder that we created, and click-and-drag the Cube into it. Now, delete the cube from the Hierarchy, but leave it be in the Prefabs folder. The red circles in the image below indicate what the encircled areas should now look like:
We now need our first of two scripts in this tutorial, a relatively simple script to move the sphere. Go back from the Prefabs folder to your main Assets folder, and open the Scripts folder. Right-click->Create->C# Script, and name it "Move". In MonoDevelop, change the code to look like this:
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour {
void Update () {
transform.position += new Vector3 (Input.GetAxis ("Horizontal"), 0, 0);
// You don't need to copy this bit, but the two numbers at the end above are zeroes.
}
void OnCollisionEnter(Collision col){
Destroy (this.gameObject);
}
}
Here's how it works: In my previous tutorial, I explained what the Update() function does, it runs every frame that the computer draws.
The next bit works like this:
"transform.position" is the current position of the GameObject the script is attached to.
For "+=", I will use an example. "x += y" means "x = x+y", which in computer terms, means change X to equal X+Y. So, this means change transform.position to whatever transform.position + new Vector3 (Input.GetAxis ("Horizontal"), 0, 0) is.
"new Vector3()" means "create a new 3-dimensional position"
"Input.getAxis("Horizontal")" means "Detect any horizontal input from arrow keys, A and D, or a joystick."
"0, 0" means that we won't be moving the sphere up, down, forward, or back.
The OnCollisionEnter() function is run whenever something collides with the sphere, and Destroy() tells the script to destroy the GameObject it is attached to.
The next bit works like this:
"transform.position" is the current position of the GameObject the script is attached to.
For "+=", I will use an example. "x += y" means "x = x+y", which in computer terms, means change X to equal X+Y. So, this means change transform.position to whatever transform.position + new Vector3 (Input.GetAxis ("Horizontal"), 0, 0) is.
"new Vector3()" means "create a new 3-dimensional position"
"Input.getAxis("Horizontal")" means "Detect any horizontal input from arrow keys, A and D, or a joystick."
"0, 0" means that we won't be moving the sphere up, down, forward, or back.
The OnCollisionEnter() function is run whenever something collides with the sphere, and Destroy() tells the script to destroy the GameObject it is attached to.
Save the script, and click-and-drag it onto the Sphere. Press Play, and then the left and right arrow keys. The sphere should move across your screen in the direction you tell it to go.
Now, we need to rain cubes from above. Create a new C# script named "Cubes" and drag it onto the Main Camera. Change the code like this:
Now, we need to rain cubes from above. Create a new C# script named "Cubes" and drag it onto the Main Camera. Change the code like this:
using UnityEngine;
using System.Collections;
public class Cubes : MonoBehaviour {
public GameObject Cube;
void Start () {
InvokeRepeating ("Spawn", 0f, 0.2f);
}
void Spawn () {
Instantiate (Cube, new Vector3 (Random.Range (-6, 6), 5, 0), Quaternion.identity);
}
}
"public GameObject Cube" creates a new place in the Inspector to click-and-drag a GameObject, and the internal name for that is "Cube".
"InvokeRepeating(Spawn(), 0, 0.2f);" repeats the "Spawn" function starting at 0 seconds, and then every 0.2 seconds after that.
In Spawn(), we Instantiate a Cube at a position which is:
X: Random number between -6 and 6.
Y: 5
Z: 0
"Quaternion.identity" means to spawn the cubes without a rotation.
"InvokeRepeating(Spawn(), 0, 0.2f);" repeats the "Spawn" function starting at 0 seconds, and then every 0.2 seconds after that.
In Spawn(), we Instantiate a Cube at a position which is:
X: Random number between -6 and 6.
Y: 5
Z: 0
"Quaternion.identity" means to spawn the cubes without a rotation.
Return to Unity, click on the Main Camera, enter the Prefabs folder at the bottom of your screen, and click-and-drag the Cube to the field of "Cubes" marked "Cube | None (GameObject)"
Save the scene, and press Play.