using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class cs01 : MonoBehaviour { float v1 = 0.3f, v2 = 1; float dx, dy, dz = 0; float rx, ry, rz = 0; int score = 0; public AudioClip au1; public Text t1; void Update() { //移動 dx = Input.GetAxis("Horizontal") * v1; //dy = Input.GetAxis("Jump") * v1; //幽靈物件,沒有重力,必須設定dy=0,不能跳 dy = 0; dz = Input.GetAxis("Vertical") * v1; //物件坐標視角移動 this.gameObject.transform.Translate(dx, dy, dz, Space.Self); //放開鍵盤就停止 //if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) //{ // this.gameObject = 0; //} //測試,以第一人稱視角,比較。Space.World/Space.Self的差別 //以世界坐標視角移動 //this.transform.Translate(dx, dy, dz, Space.World); //旋轉 rx = 0; ry = Input.GetAxis("Rotate") * v2; rz = 0; this.transform.Rotate(rx, ry, rz, Space.Self); } //碰撞 private void OnTriggerEnter(Collider other) { if (other.name == "Cylinder1" || other.name == "Cylinder2" || other.name == "Cylinder3" || other.name == "Cylinder4" || other.name == "Cylinder5") { //刪除被碰撞的物件 Destroy(other.gameObject); //播放音效 this.gameObject.GetComponent().Play(); //顯示分數 score += 20; t1.text = "分數:" + score.ToString(); //若是100分,顯示過關 if (score == 100) { this.gameObject.GetComponent().clip = au1; this.gameObject.GetComponent().Play(); t1.text = "恭喜,過關"; } } } }