Hi,
so i have a ship gameobject (sprite), and i have this script attached…
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public ParticleSystem explosion1;
public AudioSource DestroySound;
public float Speed;
public float padding = 1f;
public GameObject laserBeam;
public float laserSpeed;
public float firingRate = 0.2f;
float xmin;
float xmax;
public float health;
// Use this for initialization
void Start () {
float distance = transform.position.z - Camera.main.transform.position.z;
//camera.main is the main camera and lets us act on the main camera
Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0,0,distance ));
// we have used camera.main.viewporttoworld poin to workoout the boundaries.
Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1,0,distance ));
xmin = leftmost.x + padding;
xmax = rightmost.x - padding;
}
void Fire() {
Vector3 offset = new Vector3(0,1,0);
GameObject beam = Instantiate(laserBeam, transform.position+offset, Quaternion.identity) as GameObject ;
//Quaternion.identity means that there will be no rotation at all. return whatever it gets as gameobject
beam.rigidbody2D.velocity = new Vector3(0, laserSpeed, 0);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space)){
InvokeRepeating ("Fire", 0.000001f, firingRate);
}
if(Input.GetKeyUp(KeyCode.Space)) {
CancelInvoke("Fire") ;
}
if (Input.GetKey(KeyCode.LeftArrow)) {
transform.position += Vector3.left * Speed * Time.deltaTime;
//The time in seconds it took to complete the last frame (Read Only).
//Use this function to make your game frame rate independent.
}else if (Input.GetKey(KeyCode.RightArrow)) {
transform.position += Vector3.right * Speed * Time.deltaTime;
//+= means incriment or add to.
// VECTOR3- Representation of 3D vectors and points.
//This structure is used throughout Unity to pass 3D positions and directions around. It also contains functions for doing common vector operations.
}
//restrict the player to the game space
float newx = Mathf.Clamp(transform.position.x, xmin, xmax);
transform.position = new Vector3(newx, transform.position.y, transform.position.z);
}
void OnTriggerEnter2D(Collider2D collider){
Projectile missile = collider.gameObject.GetComponent<Projectile>();
if(missile){
Debug.Log ("Player collision");
health -= missile.getDamage();
missile.Hit ();
if (health <= 0){
DestroySound.Play();
explosion1.transform.position = this.transform.position;
explosion1.Play();
explosion1.enableEmission = true;
Destroy(gameObject);
}
Debug.Log("Hit by a laser");
}
}
}
the problem is that when health becomes 0, the audio clip does not play. please help.
thanks in advance.