Creating boundary for asteroids
Firstly a game object called boundary is created. A books Collider is added. Attached to this foundry new script will be added called destroy by boundary. The script means if the asteroid leaves the boundary it will be removed.
Adding Asteroids
An empty game object called asteroid is created.
Then I dragged the model asteroid as a child of the asteroid game object. In later development this can be changed to my own models of asteroids. There are different shaped preset colliders that we can add to our models. The collider most suitable for the shape of the asteroid would a be a capsule collider. I resized the collider so it fit around the shape of the asteroid. This means when a bolt is shot and hits the collider area the asteroid will be destroyed.
A random rotator script is added. Within the script the code allows the asteroid to tumble and rotate. I then attached it to the asteroid game object.
Boundary |
This script was also added to the asteroid game object, which allowed the option explosion to appear in the inspector. I dragged the VFX explosion_asteriod onto this. This meant every time the asteroid is hit with a bold, the asteroid gives a explosion particle effect and is destroyed.
Then I coded the player (spaceship) to be destroyed when hit/ in contract with the asteroid.
Game Controller
An empty gameobject is created and in the drop down menu Game controller is created.
The game controller will contain waves, hazards score, and ending/restarting the game.
Spawning Waves
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
public GameObject hazard;
public Vector3 spawnValues;
void Start ()
{
SpawnWaves ();
}
void SpawnWaves ()
{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
}
}
For the asteroid to be recognised as the hazard it needs to dropped onto hazard slot in the inspector. After that spawn values were set to the area that the hazards will be spawned randomly in on the x axis.
One opinion to create several asteroids to spawn in would be to copy and paste the above code several times, however this is messy, and inconvenient especially if you want to spawn in lots. Instead we added code below to give us the option to be able to change the hazard count in the inspector.
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
public GameObject hazard;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
void Start ()
{
StartCoroutine (SpawnWaves ());
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
}
}
}
using System.Collections;
public class GameController : MonoBehaviour
{
public GameObject hazard;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
void Start ()
{
StartCoroutine (SpawnWaves ());
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
}
}
}
A DestroyByTime script is added, it means the object will destroy once the time is up. This is added to all the explosions prefabs within the game.
using UnityEngine;
using System.Collections;
public class DestroyByTime : MonoBehaviour
{
public float lifetime;
void Start ()
{
Destroy (gameObject, lifetime);
}
}
using System.Collections;
public class DestroyByTime : MonoBehaviour
{
public float lifetime;
void Start ()
{
Destroy (gameObject, lifetime);
}
}
No comments:
Post a Comment