Unity - Asteroids

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
A script was then created to destroy the asteroid on contact. This id done by destroying the game object.

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

In terms of wave spawn position we want it to randomly spawn waves along the  x axis. The code below allows this.

 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.xspawnValues.x), spawnValues.yspawnValues.z);


Game Controller code in terms of spawning waves:
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.xspawnValues.x), spawnValues.yspawnValues.z);
        Quaternion spawnRotation = Quaternion.identity;
        Instantiate (hazardspawnPositionspawnRotation);
    }
}




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 = 0i < hazardCounti++)
            {
                Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.xspawnValues.x), spawnValues.yspawnValues.z);
                Quaternion spawnRotation = Quaternion.identity;
                Instantiate (hazardspawnPositionspawnRotation);
                yield return new WaitForSeconds (spawnWait);
            }
            yield return new WaitForSeconds (waveWait);
        }
    }
}


The code above also allows me to set a spawn wait time, start wait time and wave wait time, meaning the player has time to get ready at the beginning of the game, and also that the asteroid spawn in at different times and not all at once.



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 (gameObjectlifetime);
    }
}


You can change the lifetime in the inspector, which I set to 5. Now the clones do appear in the scene.

No comments: