More like chapter 14. Add two new labels. Game Over, Restart. Add references in the GameController object too. They are both blank at the start of the game. They change when game over occurs.
Unity Space Shooter – Chapter 14 – Score Inside and Outside
Inside
You’ve seen it before. Add these publics to the the GameController.
Then Add the components on the screen, then drag and drop.
Include UpdateScore() in the Start routine.
Outside
Make a public addscore routine in game controller
In any routine that needs to add a score, get a reference to the GameController, then at the right time, call the new routine. ( the AddScore() routines above doesnt match below, but you get the picture.)
Unity Space Shooter – Chapter 13 – Sounds
Music… Drag and drop the music to the GameController Object(play on awake, loop)
Drag and drop audio into the prefabs like explosion and you don’t have to program anything.
Then drag and drop the audio clip and confirm “play on awake” is checked.
That takes care of explosions. Now shooting.
Drag and drop the audio clip onto the player object.(no play on awake)
Unity Space Shooter – Chapter 12 – Waves of Asteroids(Coroutine!)
We a single control by adding loops
while(!gameOver)
for(i=0;i<#per4 wave; i++)
spawn rock
Part 2 – Removing those explosions after a few seconds
- Create a “DestroyByTime” Object
- Attach that to the prefab for each of the explosions.
Unity Space Shooter – Chapter 11 one random asteroid(GameController)
Game Controller (spawning waves)
It sits at the top of the game view as a new object with a script called “GameController”
At start it shoots off a random asteroid. We will
Creat the script with two params (where to launch the asteroid) and what to launch(asteroid prefab)
Unity Space Shooter – Chapter 10 – asteroid collision
Asteroid continued – Now add pretty explosions including the fatal player collision.
Expand on the collision script…
To make it blow up by spawning an explosion
Using tag to identify Player, and have it blow up
Then reuse the Movement code by dragging and dropping it onto the Asteroid. then setting speed in negative direction.
Unity Space Shooter – Chapter 9 – Asteroid prefab(rotation)
Create Opponent
- Setup a template asteroid object (chapter 8)
- Save it as a Prefab (chapter 9)
- Expand programming so it happens in groups
Part 1
Create Placeholder Game Object called Asteroid(reset transforms, change z above player object so we can see them relative to each other)
Drag and drop the model for an asteroid into it(reset transforms)
[ This continues the pattern of Parent object holds the program, child is what to render.]
Back at “Asteroid” Add rigidbody(no gravity, no angular drag) , Capsule collider and scale it.
Back at “asteroid” add a new c# program with “start” routine so we can set a random tumble for the object when it starts
That takes care of start. But what about hit? New Object for that that has a collider trigger like the boundary(but enter not exit variety)
So on the same “asteroid” object, create another c# script called DestroyByContact
Debug.Log(other.name);
Unity Space Shooter – Chapter 8 – boundary collider
Create a boundary around the game play area so the shots can be removed from play after they fall off the field.
- Create a box larger than the play area
- Create Cube
- Scale it
- Make sure it has a box collider
- isTrigger is checked
- Remove the Cube(mesh filter)
- Remove the mesh renderer.
- Create script(to handle the trigger) DestroyByBoundary
- Edit and add the OnTriggerExit routine. When triggered, it will destroy the other object.
- Easy
Unity Space Shooter – Chapter 7 – fire laser shot from player
Shooting shots.
Create an game object called “shotSpawn” as a child of our player. This will become the origin for the shot when started, but we need a way to communicate between the object and the c# program, so we make a couple helpers
Edit “PlayerController”
Object:
Positon:
Rotation:
We need a way to fill those in.
In the c#
public GameObject shot; // what gets shot
public Transform shotSpawn; // the object to use to get the shot
In Unity
Make an empty game object called ShotSpawn as a child of the Player object. Adjust it to the tip of the nose. This is where the initial location comes from.
Drag the bullet from the Prefabs into the new “shot” reference
Drag the ShotSpawn into the new shotSpawn reference
That is the setup.
Now to shoot.
Instantiate the shot when button is clicked (but include a pause) so we dont fire one every frame
Unity Space Shooter – Chapter 6 – create laser prefab
Shot fire Object
A lot going on.
We create a game object called “Bolt” and add a child object(quad) called VFX. We make this two parts so we can separate the logic from rendering.
- Game Object called “Bolt” This will be our primary shot object. The bolt object has movement(rigid body) and collision detection(capsule collider)
- Child Game object called VFX(quad) (transform rotate 90 to see)
- Create a material
- Go to material folder
- Create new new material and name it fx_bolt_orange
- Use the “select” picker to attach the texture of the bolt
- Select shading as mobile/particles/additive
- Drag and drop new material onto Quad
- Setup collision
- Remove “mesh collider” from VFX object(there by default)
- Go to Bolt and add component physics/capsule collider
- Adjust the size
- Change Direction to z-axis
- Select “isTrigger” checkbox to make it a “trigger collider”
- Attach a new Script called “Mover”
- see the new public “speed” set that on the Bolt object(20 is ok)
- Test by playing.
- Move it to Prefab
- Test by playing and drag and drop as often as you want.
Create new Material
fx_bolt_orange