using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class cs01 : MonoBehaviour { //移動倍率 float v1 = 3.5f; //移動距離 float dx, dy, dz = 0; //分數 int score = 0; //win sound effect public AudioClip au1; void Update() { Vector3 vel; //移動 //(2)判斷鍵盤ADWS則移動➜設定vel.x = ....,vel.y = ....(可以用2種方式:Input.GetAxis, Input.GetKeyDown)< br > vel.x = v1 * Input.GetAxis("Horizontal"); vel.z = v1 * Input.GetAxis("Vertical"); //(3)鍵盤放開則停止移動➜判別鍵盤放開Input.GetKeyUp則vel.x = 0.... < br > if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D)) { vel.x = 0; } if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S)) { vel.z = 0; } //(4)有向上跳躍的力學效果➜按space鍵則使用addforce移動(但沒有設定vel.y)< br > if(Input.GetKeyDown(KeyCode.Space)) { this.gameObject.GetComponent().AddForce(Vector3.up*400); } //(5)注意:要設定vel.y(= this.gameObject.GetComponent().velocity.y vel.y = this.gameObject.GetComponent().velocity.y; //(1)如何能夠連續移動➜在update內設定幀動畫移動公式➜剛體.velocity=vel,才能控制鍵盤放開讓速度 = 0)(不能使用transform.TransLate, 剛體.AddForce)< br > this.gameObject.GetComponent().velocity = vel; } //碰撞 private void OnCollisionEnter(Collision collision) { if (collision.gameObject.name == "Cylinder1" || collision.gameObject.name == "Cylinder2" || collision.gameObject.name == "Cylinder3") { Destroy(collision.gameObject); score += 30; GameObject.Find("Tscore").GetComponent().text = "分數:" + score.ToString(); this.gameObject.GetComponent().Play(); if (score == 100) { GameObject.Find("Tscore").GetComponent().text = "恭喜,過關"; this.gameObject.GetComponent().clip = au1; this.gameObject.GetComponent().Play(); } } } //觸發 private void OnTriggerEnter(Collider other) { if (other.name == "Cylinder4" || other.name == "Cylinder5") { Destroy(other.gameObject); score += 5; GameObject.Find("Tscore").GetComponent().text = "分數:" + score.ToString(); this.gameObject.GetComponent().Play(); if (score == 100) { GameObject.Find("Tscore").GetComponent().text = "恭喜,過關"; this.gameObject.GetComponent().clip = au1; this.gameObject.GetComponent().Play(); } } } }