Hi.
I have a problem. I’m making a game where there are pixel art ‘BOMBS’ as enemies.
My player dies when he hit the bomb, and exploding animation plays, and an AudioClip should play once.
However, the AudioClips starts repeating itself an infinite amount of times, resulting in a sound that sounds like my computer is stuck.
Here is the code attached to the bomb.
using UnityEngine;
using System.Collections;
public class bombScript : MonoBehaviour {
Animator animator;
SpriteRenderer bomb;
static public bool hitbomb = false;
GameObject dragon;
AudioSource bombAudio;
public AudioClip bombClip;
// Use this for initialization
void Start () {
animator = gameObject.GetComponentInChildren<Animator> ();
dragon = GameObject.FindGameObjectWithTag ("Player");
bombAudio = GameObject.Find ("SoundManager/bombAudio").GetComponent<AudioSource> ();
}
void OnTriggerEnter2D(Collider2D player) {
if(dragon.GetComponent<beta_DragonMovement>().Godmode == true)
return;
if (player.tag == "Player") {
bombAudio.PlayOneShot (bombClip);
bomb = gameObject.GetComponent<SpriteRenderer> ();
bomb.enabled = false;
hitbomb = true;
animator.SetTrigger ("death");
}
}
}
I also tried the following script:
It resulted in the same problem.
Wah wah.
using UnityEngine;
using System.Collections;
public class bombScript : MonoBehaviour {
Animator animator;
SpriteRenderer bomb;
static public bool hitbomb = false;
GameObject dragon;
AudioSource bombAudio;
// Use this for initialization
void Start () {
animator = gameObject.GetComponentInChildren<Animator> ();
dragon = GameObject.FindGameObjectWithTag ("Player");
bombAudio = GameObject.Find ("SoundManager/bombAudio").GetComponent<AudioSource> ();
}
void OnTriggerEnter2D(Collider2D player) {
if(dragon.GetComponent<beta_DragonMovement>().Godmode == true)
return;
if (player.tag == "Player") {
bombAudio.Play ();
// the bombClip is pre-set in the inspector.
bomb = gameObject.GetComponent<SpriteRenderer> ();
bomb.enabled = false;
hitbomb = true;
animator.SetTrigger ("death");
}
}
}