It depends on how you’re implementing the fireball. A simple “fireball recipe”: create an empty object (“Fireball”) and add to it a sphere collider, the particle effect, an AudioSource and a kinematic rigidbody (set Is Kinematic in the Inspector), then set the collider’s Is Trigger field in the Inspector to make it a trigger. Tag this object as “Fireball” and add the script below to it:
using UnityEngine;
using System.Collections;
public class Fireball: MonoBehaviour {
public float speed = 8.0f;
public AudioClip hitSound; // add a hit sound, if desired
void Start(){
Destroy(gameObject, 6); // suicide in 6 seconds
}
void Update(){
// move the fireball forward at speed units/second
transform.Translate(0, 0, speed * Time.deltaTime);
}
void OnTriggerEnter(){ // hit something:
// play the hit sound, if any...
if (hitSound) AudioSource.PlayClipAtPoint(hitSound, transform.position);
Destroy(other.gameObject); // and die
}
}
If you set the AudioSource Clip property to some sound, the fireball will play it when instantiated (Play On Awake is set by default). You can also add a hit sound to the field Hit Sound in the Inspector.
Finally, drag this object to the Project view to make it a prefab.
Now, the door: add a script like below to the door prefab, so that it will destroy itself when some trigger tagged “Fireball” hits it:
using UnityEngine;
using System.Collections;
public class FireballDoor: MonoBehaviour {
void OnTriggerEnter(Collider other){
if (other.tag == "Fireball"){
Destroy(gameObject);
}
}
}
You may of course elaborate the OnTriggerEnter function so that it makes more interesting things when hit - like sinking in the ground with a convenient sound, for instance:
...
public class FireballDoor: MonoBehaviour {
public float speed = 2.5f; // sinking speed in units/second
public float doorHeight = 1.8f; // the door height
IEnumerator OnTriggerEnter(Collider other){ // this is a coroutine now!
if (other.tag == "Fireball"){ // if hit by a fireball...
float endY = transform.position.y - doorHeight;
if (audio) audio.Play(); // if some audio effect, play it
while (transform.position.y > endY){ // repeat until door completely sunk
transform.position -= new Vector3(0, speed * Time.deltaTime, 0);
yield return null;
}
}
}
You should add an AudioSource to the door, and set its Clip property to some suitable audio clip in order to have a nice “sinking” sound.