using System.Collections; using System.Collections.Generic; using UnityEngine; public class bullet_logic : MonoBehaviour { [Tooltip("子彈飛行速度")] public float speed = 50.0f; [Tooltip("子彈生命時長")] public float lifetime = 30; [Tooltip("子彈爆炸prefab")] public GameObject bulletExplosion; void Start() { Invoke("selfdestory", lifetime); } void Update() { this.transform.Translate(0, 0, 1.0f * speed * Time.deltaTime, Space.Self); } private void OnTriggerEnter(Collider other) { //刪除this,other if (other.gameObject.name != "Monster(Clone)") return; //刪除物件 Destroy(this.gameObject); Destroy(other.gameObject); //產生爆炸預鑄體prefab,放在根目錄(null) GameObject node1 = Instantiate(bulletExplosion, null); //播放爆炸音效 //注意:爆炸音效要放在爆炸預鑄件prefab,不要放在子彈或主角(因為被destory了,就無法播放音效) node1.GetComponent().Play(); //設定爆炸的位置 node1.transform.position = this.transform.position; } void selfdestory() { Destroy(this.gameObject); } }