using System.Collections; using System.Collections.Generic; using UnityEngine; public class cs01 : MonoBehaviour { //剛體移動的速度向量vel Vector3 vel; void Update() { //1.移動有三種方式(transform.Translate, 剛體.AddForce,剛體.velocity) //2.完美移動演算法:必須使用剛體.velocity來移動(因為鍵盤放開就要設定速度vel.x=0,只有velocity才能設定速度=0,其它方法都不能) //3.完美移動演算法5步驟: //(1)如何能夠連續運動➜在update內設定幀動畫移動公式➜剛體.velocity=vel,才能控制鍵盤放開讓速度 = 0)(不能使用transform.TransLate, 剛體.AddForce) //(2)判斷鍵盤ADWS則移動➜設定vel.x = ....,vel.y = ....(可以用2種方式:Input.GetAxis, Input.GetKeyDown) //(3)如何鍵盤停止則移動停止➜判別鍵盤放開Input.GetKeyUp則vel.x = 0.... //(4)如何能夠有向上跳躍的力學效果➜space鍵盤則使用addforce(但沒有設定vel.y) //(5)注意:要設定vel.y(= this.gameObject.GetComponent().velocity.y //------------------------------------------------------- //(2)判斷鍵盤則移動(可以用2種方式:Input.GetAxis, Input.GetKeyDown) vel.x = Input.GetAxis("Horizontal") * 5; vel.z = Input.GetAxis("Vertical") * 5; //(3)如何鍵盤停止則移動停止:判別鍵盤放開Input.GetKeyUp則vel.x=0.... 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)如何能夠有向上跳躍的力學效果:使用addforce(但沒有設定vel.y) if (Input.GetKeyDown(KeyCode.Space)) { this.gameObject.GetComponent().AddForce(Vector3.up * 350); } //(5)補充設定vel.y = = this.gameObject.GetComponent().velocity.y vel.y = this.gameObject.GetComponent().velocity.y; //(1)如何能夠連續運動:在update內設定幀動畫移動公式 this.gameObject.GetComponent().velocity = vel; } }