using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class cs01 : MonoBehaviour { int score = 0; public AudioClip au1; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { Vector3 vel; //(1) 5 steps to move //1.vel.x, vel.y(世界座標) vel.x = Input.GetAxis("Horizontal")*5; vel.z = Input.GetAxis("Vertical") *5; //6.轉成局部座標合成向量localvec; //Vector3.right, Vector3.forward(世界座標單位向量) //transform.right, transform.forward(局部座標單位向量) Vector3 localvec; localvec = transform.right * vel.x + transform.forward * vel.z; vel.x = localvec.x; vel.z = localvec.z; //2.keyup(A)==> vel.x = 0 if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow)) { vel.x = 0; } if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.UpArrow) || Input.GetKeyUp(KeyCode.DownArrow)) { vel.z = 0; } //3.space ==> addforce(up) if(Input.GetKeyDown(KeyCode.Space)) { this.gameObject.GetComponent().AddForce(Vector3.up*300); } //4.vel.y = vel.y = this.gameObject.GetComponent().velocity.y; //5.velocity this.gameObject.GetComponent().velocity = vel; //(2).第一人稱視角,球會跟著滾動 //解決方法:限制剛體的轉動角度-->constraints //(3)rotation Vector3 rot; rot.x = 0; rot.z = 0; rot.y = Input.GetAxis("Rotate"); this.gameObject.transform.Rotate(rot); } private void OnTriggerEnter(Collider other) { if(other.gameObject.name == "Cylinder (1)" || other.gameObject.name == "Cylinder (2)" || other.gameObject.name == "Cylinder (3)" || other.gameObject.name == "Cylinder (4)" || other.gameObject.name == "Cylinder") { this.gameObject.GetComponent().Play(); score += 20; GameObject.Find("Text").GetComponent().text = score.ToString() + "分"; if(score ==100) { GameObject.Find("Text").GetComponent().text = "恭喜,过关"; this.gameObject.GetComponent().clip = au1; this.gameObject.GetComponent().Play(); } Destroy(other.gameObject); } } }