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; // [Tooltip("子彈爆炸音效")] //public AudioSource bulletExplosionSound; void Start() { Invoke("selfdestory", lifetime); } // Update is called once per frame void Update() { this.transform.Translate(0, 0, 1.0f * speed * Time.deltaTime, Space.Self); //this.transform.Translate(-1.0f * speed * Time.deltaTime, 0, 0, Space.Self); } private void OnTriggerEnter(Collider other) { //刪除this,other if (other.gameObject.name != "Monster(Clone)") return; //Debug.Log("碰撞了 " + other.gameObject.name); //播放爆炸音效 //注意:爆炸音效要放在爆炸預鑄件prefab,不要放在子彈或主角(因為被destory了,就無法播放音效) //bulletExplosionSound.Play(); //刪除物件 Destroy(this.gameObject); Destroy(other.gameObject); //產生爆炸預鑄體prefab,放在根目錄(null) GameObject node1 = Instantiate(bulletExplosion, null); //播放爆炸音效 //注意:爆炸音效要放在爆炸預鑄件prefab,不要放在子彈或主角(因為被destory了,就無法播放音效) //node1.AddComponent(); //node1.GetComponent().clip = bulletExplosionSound.clip; node1.GetComponent().Play(); //設定爆炸的位置 node1.transform.position = this.transform.position; } void selfdestory() { Destroy(this.gameObject); } }