using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; public class playerLogic : MonoBehaviour { [Tooltip("子彈實體")] public GameObject bulletPrefab; [Tooltip("子彈目錄")] public Transform bulletFolder; [Tooltip("子彈間隔")] public float bulletInterval = 0.5f; [Tooltip("子彈開火點")] public Transform bulletFirePoint; [Tooltip("玩家移動速度")] public float playerSpeed = 1.0f; [Tooltip("子彈爆炸prefab")] public GameObject bulletExplosion; //fire public void FireOn() { //定時器,每隔0.1s秒,開火一次 //Invoke("fire", bulletInterval, bulletInterval); InvokeRepeating("fire", 0.01f, bulletInterval); } public void FireOff() { //關閉某個定時器(執行fire函數) CancelInvoke("fire"); //關閉全部定時器 //CancelInvoke(); } // Update is called once per frame void Update() { if(Input.GetKey(KeyCode.A)) { //(1)用translate慢慢位移 //this.transform.Translate(0, 0, -1*playerSpeed * Time.deltaTime); //this.transform.Translate(new Vector3(0, 0, -5) * Time.deltaTime); //this.transform.Translate(Vector3.back * Time.deltaTime); //this.transform.Translate(Vector3.back * Time.deltaTime, Space.Self); //this.transform.Translate(Vector3.back * Time.deltaTime, Space.World); //this.transform.Translate(Vector3.back * Time.deltaTime, Camera.main.transform); //(2)用positino瞬間移動 //注意:Space.Self只針對Translate才能用,但是不能用在position //this.transform.position -= new Vector3(0, 0, 1); //this.transform.position += new Vector3(0, 0, -1); this.transform.position += Vector3.back* playerSpeed; } else if(Input.GetKey(KeyCode.D)) { //this.transform.Translate(Vector3.forward * Time.deltaTime*playerSpeed); this.transform.position += Vector3.forward* playerSpeed; } } private void OnTriggerEnter(Collider other) { if (other.gameObject.name != "Monster(Clone)") return; //產生爆炸預鑄件 GameObject node1 = Instantiate(bulletExplosion, null); node1.transform.position = this.transform.position; //播放爆炸音效 //注意:爆炸音效要放在爆炸預鑄件prefab,不要放在子彈或主角(因為被destory了,就無法播放音效) node1.GetComponent().Play(); //銷毀玩家,與怪物 Destroy(this.gameObject); Destroy(other.gameObject); } void fire() { GameObject node = Instantiate(bulletPrefab, bulletFolder); node.transform.position = bulletFirePoint.position; } }