Now create a new script, the reason for this is to create a
script that will destroy the asteroid explosions since they are permanent since
they cant be destroyed by boundary or trigger, name the script DestroyByTime
Public class DestroyByTime : MonoBehaviour
{
Public float lifetime;
Void Start ()
{
Destory (gameObject, lifetime);
}
}
Back in unity prefabs VFX explosions select the explosion
asteroid, add component, scripts and select DestroyByTime then set the lifetime to 2, do the same for
the remaining explosion prefabs this means that the explosions are no longer
crowding the scene which will have an impact on performance should things get
out of hand, this code helps keep it in order.
Audio is a large part of video games especially when it comes
to immersion and can make a game, of course I can’t make the audio from scratch
but using unity store assets I picked a few pieces of audio to use in this
project. Select the explosion asteroid prefab so that you can see the inspector
then drag the audio clip named explosion asteroid into the asteroid prefabs
inspector this will create a audio source and reference to the audio source in
the prefabs inspector, make sure play on awake is selected/checked so that the
clip is played when explosion is played. Now do the same for the player, drag
the desired weapon audio clip onto the player game object, and make sure play
on awake is not selected then open the player controller script.
[System.Serializable]
Public class Boundary
{
Public float xMin, xMax, zMin, zMax;
}
Public class PlayerController : MonoBehaviour
{
Public float speed;
Public float tilt;
Public Boundary boundary;
Public GameObject shot;
Public Transform shotSpawn;
Public float fireRate;
Private float nextFire;
Void update ()
{
If (Input.GetButton(“Fire1” && Time.time >
nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
GetComponent<AudioSource>().Play ();
}
}
Void FixedUpdate ()
{
Float moveHorizontal = Input.getAxis (“Horizontal”;
Float moveVertical = Input.getAxis (“Vertical”;
Vector3 movement = new Vector3 (moveHorizon, 0.0f,
moveVertical);
GetComponent<rigidbody>().velocity = movement * speed;
GetComponent<rigidbody>().position = new Vector3
(
Mathf.Clamp (GetComponent<rigidbody>().position.x,
boundary.xMin, boundary.xMax), 0.0f,
Mathf.Clamp (GetComponent<rigidbody>().position.z, boundary.zMin,
boundary.zMax)
);
GetComponent<rigidbody>().rotation = Quaternion.Euler
(0.0f, 0.0f, GetComponent<rigidbody>().velocity.x * -tilt);
}
}
Now for the games background music for this drag whatever
music onto the game controller object making sure it is set to play on awake so
it plays immediately and that Loop is also selected so that it repeats when the
track would normally end.
Now for some GUI text for a score system and so on. Create a
new empty game object add a gui text component from the add component button
then in the transform position set it to 0.0, 1, 0.0 then next to where it says
Text enter Score Text. Then using the pixel offset which basically says how
much space do you want around the text, set X to 10 and Y to -10. Now in the
GameController script.
Public class GameController : MonoBehaviour
{
Public GameObject hazard;
Public Vector3 spawnValues;
Public inthazardCount;
Public float spawnWait;
Public float startWait;
Public float waveWait;
Public guiText scoreText;
Private int score;
Void start ()
{
Score = 0;
UpdateScore ();
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);
}
}
Public void AddScore (int newScoreValue)
{
Score += newScoreValue;
UpdateScore ();
}
Void UpdateScore ()
{
scoreText.text = “score: “ + score;
}
}
Now to assign a score value to the asteroids, open up the
DestroyByContact script
Public class DestroyByContact : MonoBehaviour
{
Public GameObject explosion;
Public GameObject playerExplosion;
Public int scoreValue;
Private GameController gameController;
Void Start ()
{
gameObject gameControllerObject = GameObject.FindWithTag
(“GameController”);
if (gameControllerObject != null)
{
gameController =
gameControllerObject.GetComponent<GameController>();
}
If (gameController == null)
}
Void onTriggerEnter(Collider other) {
If (other.tag == ”Boundary”)
{
Return;
}
Instantiate(explosion, transform.position,
transform.rotation);
If (other.tag == “Player”){
Instantiate(playerExplosion, other.transform.position,
other.transform.rotation);
}
gameController.AddScore (scoreValue);
Destroy(other.gameObject);
Destory(gameObject);
}
}
Now select the game controller in the hierarchy then drag
the score text object into the Score Text slot in the inspector and now when
each asteroid is destroyed the player gains 10 points. Next is to create an
empty parent to hold the text labels such as the score and the two new game
objects name one of the new objects Display text and reset its transform, then
add score text to Display text so that score text is a child of display text,
add another game object and name it Restart text and set its transform like so 1, 1, 0 and change the
Anchor properties from upper left to upper right and the alignment to right and
now we change the pixel offset by using -10 and -10, drag the restart text
object into the display text to make it a child of display text. Then create
new object and name it GameOvertext, making sure the Text is set to Game Over
Text and that the anchor is set to middle center and the transforms position is
set to 0.5, 0.6, 0 and like the others add Game Over Text object to display
Text to make it a child of Display Text.
Now I need to change the Game Controller script.
Public class GameController : MonoBehaviour
{
Public GameObject hazard;
Public Vector3 spawnValues;
Public inthazardCount;
Public float spawnWait;
Public float startWait;
Public float waveWait;
Public GUIText scoreText;
Public GUIText restartText;
Public GUIText gameOverText;
Private bool gameOver;
Private bool restart;
Private int score;
Void start ()
{
gameOver = false;
restart = false;
restartText.text = “”;
gameOverText.text =””;
Score = 0;
UpdateScore ();
StartCoroutine (SpawnWaves ());
}
Void Update ()
{
If (restart)
{
If (Input.GetKeyDown (KeyCode.R))
{
Application.LoadLevel (Application.LoadedLevel);
}
}
}
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);
If (gameOver)
{
restartText.text = “Press ‘R’ for Restart”;
restart = true;
break;
}
}
}
Public void AddScore (int newScoreValue)
{
Score += newScoreValue;
UpdateScore ();
}
Void UpdateScore ()
{
scoreText.text = “score: “ + score;
}
Public void GameOver ()
{
gameOvertext.text = “Game Over!”;
gameOver = true;
}
}
Now with game controller selected drag Restart Text into the
Restart Text slot in the game controller inspector and do the same with the
game over text, drag it into the game over text slot.
Now I need to edit the Destroy by contact script.
Public class DestroyByContact : MonoBehaviour
{
Public GameObject explosion;
Public GameObject playerExplosion;
Public int scoreValue;
Private GameController gameController;
Void Start ()
{
gameObject gameControllerObject = GameObject.FindWithTag
(“GameController”);
if (gameControllerObject != null)
{
gameController =
gameControllerObject.GetComponent<GameController>();
}
If (gameController == null)
}
Void onTriggerEnter(Collider other) {
If (other.tag == ”Boundary”)
{
Return;
}
Instantiate(explosion, transform.position,
transform.rotation);
If (other.tag == “Player”)
{
Instantiate(playerExplosion, other.transform.position,
other.transform.rotation);
gameController.GameOver ();
}
gameController.AddScore (scoreValue);
Destroy(other.gameObject);
Destory(gameObject);
}
}
Now we have a working game about shooting asteroids in
SPAAAAAAACE! If I get time ill add specific enemies that attack the player but
that will be extremely last minute if I do get the chance.
Comments
Post a Comment