Unity – Spaceship Shooting Game – JS – Audio
Asteroid Explosion
Project> Assets> Audio> explosio_asteroid.wav DRAG AND DROP over Prefabs> VFX> Explosions> explosion_asteroid
Prefabs> VFX> Explosions> explosion_asteroid> Inspector> Audio Source> check ‘Play on Awake’, the wav it will be played automatically.
When the VFX explosion_asteroid is instanced the Audio Sopurce Component will play xplosio_asteroid.wav automatically.
Player Explosion
The same procedure as above
Weapon Player
Project> Assets> Audio> weapon_player.wav DRAG AND DROP over Hierarchy> Player
Hierarchy> Player> Audio Source> uncheck ‘Play on Awake’
Hierarchy> Player> PlayerController.js
#pragma strict class Boundary { // Theese variables are public to make the code easy-reusable // You can setup theese variables from Inspector var xMin : float; var xMax : float; var zMin : float; var zMax : float; } var speed : float; var tilt : float; var boundary : Boundary; var shot : GameObject; // Inspector -> assign Bolt Prefab var shotSpawn : Transform; // Inspector -> assign Shot Spawn Empty Object var fireRate : float; // Inspector -> 0.25 (seconds) = 4 shot per second private var nextFire : float; function Update () { // Get Input Fire button if (Input.GetButton("Fire1") && Time.time > nextFire) { nextFire = Time.time + fireRate; // Return the clone of the GameObject, position, rotation if user click Fire button Instantiate(shot, shotSpawn.position, shotSpawn.rotation); audio.Play (); } } function FixedUpdate () { // Get User Input START var moveHorizontal : float= Input.GetAxis ("Horizontal"); var moveVertical : float= Input.GetAxis ("Vertical"); // Get User Input END // Move the GameObject var movement : Vector3= new Vector3 (moveHorizontal, 0.0f, moveVertical); rigidbody.velocity = movement * speed; // Limitate movement inside the screen START rigidbody.position = new Vector3 ( Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax) ); // Limitate movement inside the screen END // Tilt movement START rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt); // Tilt movement END }
Notice:
if (Input.GetButton("Fire1")... ... audio.Play (); } ...
Audio play… if fire button is pressed!
Background Music
Project> Assets> Audio> music_background.wav DRAG AND DROP over Hierarchy> Game Controller
Hierarchy> Game Controller> Audio Source> check ‘Play on Awake’, check ‘Loop’
Balacing Audio
To setup audio volume open the Audio Source Component> Volume, setup this.