Good day!
I would like to play a music when my runner collides with another game object “food”.
The music is blips. I created " public AudioClip blips;" in my code and added
" AudioSource.PlayClipAtPoint(blips[0],transform.position);" after the “void OnCollisionEnter2D (Collision2D other) {”. The problem is, when I played my game, the music was not played as expected. May I know what went wrong with the code?
here is the entire code for you reference. I appreciate your response!
using UnityEngine;
using System.Collections;
public class runner : MonoBehaviour {
public Vector2 jumpForce = new Vector2(0, 1);
public AudioClip[] blips;
void Update () {
if (Input.GetKeyUp("space")) {
rigidbody2D.velocity = Vector2.zero;
rigidbody2D.AddForce(jumpForce);
}
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
Die();
}
void OnCollisionEnter2D (Collision2D other) {
if (other.gameObject.tag == "food")
AudioSource.PlayClipAtPoint(blips[0],transform.position);
Destroy (other.gameObject);
else
Die();
}
void Die () {
Destroy(this.gameObject);
Application.LoadLevel(Application.loadedLevel);
}
}