Aerial combat
Plan:
-Expand on "Cubes!" and turn it into an aerial combat simulator
-Expand on "Cubes!" and turn it into an aerial combat simulator
Prerequisites:
Tutorial 4: "Cubes!"
Tutorial 4: "Cubes!"
Open "Cubes!"
Today, we're going to turn it into a very basic version of this:
That's right, Mighty Games's Shooty Skies. While it may look complicated at first, when I encountered this game, it occurred to me that is was based on exactly the same premise as "Cubes!". Objects are hurled at the player, and they must either destroy them or dodge them.
First, we need to move the sphere to (0, 0, 0). Then, move the camera to the position in the image and set "Clear Flags" to "Skybox".
Open the "Cubes" script and make the following changes (in bold italics):
void Start () {
InvokeRepeating ("Spawn", 0f, 0.4f);
}
void Spawn () {
Instantiate (Cube, new Vector3 (Random.Range (-6, 6), 0, 15), Quaternion.identity);
}
Explanation:
In the Start() function, we changed 0.2f to 0.4f to slow down the spawn of cubes.
In the Spawn() function, we changed the Y and Z position of the spawn location, to make the cubes spawn at the same height as the player and 15 metres in front of it.
In the Start() function, we changed 0.2f to 0.4f to slow down the spawn of cubes.
In the Spawn() function, we changed the Y and Z position of the spawn location, to make the cubes spawn at the same height as the player and 15 metres in front of it.
Now, pressing Play will reveal that the cubes spawn and fall, 15 metres in front of the player. We need them to come at the player. For this, we need to disable the "Use Gravity" field in the Cube prefab's Rigidbody. (Open the Prefabs folder, click "Cube", and make the change in the Inspector) We also need to write a new script, which we will call "CubeMove", to fire the blocks at the player.
using UnityEngine;
using System.Collections;
public class CubeMove : MonoBehaviour {
public float MinForce;
public float MaxForce;
// Use this for initialisation
void Start () {
GetComponent<Rigidbody>().AddForce(new Vector3(0, 0, Random.Range (-MinForce, -MaxForce)));
}
}
We should be familiar with AddForce from our Character Controller tutorial, so I will omit the explanation. Add the new script to the Cube prefab, set MinForce to 200 and MaxForce to 300, and click Play.
It works, but the player can still fly off into infinity. Add two cubes, one at (-7, 0, 0) and one at (7, 0, 0). It should look something like this from the Scene view:
It works, but the player can still fly off into infinity. Add two cubes, one at (-7, 0, 0) and one at (7, 0, 0). It should look something like this from the Scene view:
It does nothing. The player can still freely move through them. We need to add a function to our Move script:
void OnTriggerEnter(Collider col){
transform.position = Vector3.zero;
}
We also need to click on each Cube and set its "Is Trigger" under the Box Collider to true. Remember to do this to both cubes! Also, add a Rigidbody to the Sphere. Be sure to switch off "Use Gravity"!
We now have the essentials of Shooty Skies, except the shooting bit. Create a new script, call it "Shoot", and add it to the sphere.
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour {
public GameObject bullet;
// Use this for initialisation
void Start () {
InvokeRepeating ("shoot", 0f, 0.25f);
}
void shoot () {
Instantiate (bullet, Vector3.forward + transform.position, Quaternion.identity);
}
}
There should be nothing in here that you've never seen before...
Create a new sphere, rename it "bullet", and change its Inspector to look like this:
Click-and-drag "bullet" from the Hierarchy to your Prefabs folder, add a Rigidbody (no gravity!) to the prefab, and delete the instance of it in the Hierarchy. Now, open the Player's Inspector and click-and-drag "bullet" from the Prefabs folder to where it says "bullet | None (GameObject)" in our new script.
Now, we need yet another script to tell the bullets what to do. Create a new script, and call it "BulletBehaviour".
using UnityEngine;
using System.Collections;
public class BulletBehaviour : MonoBehaviour {
// Use this for initialisation
void Start () {
GetComponent<Rigidbody> ().AddForce (Vector3.forward * 400f);
Destroy (this.gameObject, 5f);
}
void OnCollisionEnter (Collision col) {
Destroy (col.gameObject);
}
}
The only thing new to you here should be the Destroy in Start(), which has an extra argument. It is told to destroy this.gameObject, after 5 seconds. This is done to prevent thousands of bullets from wrecking our framerate. Open the "bullet" prefab, click "Add Component", and type in "bullet". The first option should be our new BulletBehaviour script.
And just like that... a very, very basic version of Shooty Skies.