Unity 3D坦克大战案列学习(二)
实现子弹的爆炸
using UnityEngine;
using System.Collections;
/// <summary>
/// 控制子弹爆炸特效
/// </summary>
public class Shell : MonoBehaviour {
public GameObject shellExplosionPrefab;//子弹爆炸特效
public AudioClip shellExplosionAudio;//子弹爆炸的音效
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
public void OnTriggerEnter(Collider collider)//检测碰撞
{
//调用子弹爆炸音效
AudioSource.PlayClipAtPoint(shellExplosionAudio,transform.position);
//实例化特效
GameObject.Instantiate(shellExplosionPrefab, transform.position, transform.rotation);
GameObject.Destroy(this.gameObject);//表示把自身销毁
if (collider.tag == "Tank")//当特效检测碰撞到"Tank"层级
{
collider.SendMessage("TakeDamage");//给坦克发送受到伤害的事件消息
}
}
}