using System.Collections; using System.Collections.Generic; using UnityEngine; public class cs01 : MonoBehaviour { //移動距離變數dx,dy,dz float dx, dy, dz = 0; //旋轉變數rx,ry,rz float rx, ry, rz = 0; //縮放力道倍率參數v1 float v1 = 0.2f; void Update() { //1.左轉e,右轉r(要在Input.Manager裡面新增第19個輸入項目:Rotate) rx = 0; ry = Input.GetAxis("Rotate"); rz = 0; this.gameObject.transform.Rotate(rx, ry, rz); //2.鍵盤移動: wsad+space跳,按越久力道越大(會感測你鍵盤按到力道,而做出移動距離) dx = Input.GetAxis("Horizontal") * v1; dz = Input.GetAxis("Vertical") * v1; dy = Input.GetAxis("Jump") * v1; //this.gameObject.transform.Translate(dx, dy, dz); //3.問題:鍵盤若是一直按著,則會一直加速,直到失控 //印出dx,dy,dz1的數量(用Debug.log()或是print(),都可以在控制台console輸出資料) //Debug.Log("dx=" + dx); //Debug.Log("dy=" + dy); //Debug.Log("dz=" + dz); print("dx=" + dx); print("dy=" + dy); print("dz=" + dz); //4.解決方法:加速若dx若超過maxVx,則dx = maxVx(限制速度,訂定速度上限) float maxVx = 0.05f; float maxyx = 0.05f; float maxVz = 0.05f; if (dx > maxVx) dx = maxVx; if (dx < -maxVx) dx = -maxVx; if (dy > maxyx) dy = maxyx; if (dy < -maxyx) dy = -maxyx; if (dz > maxVz) dz = maxVz; if (dz < -maxVz) dz = -maxVz; this.gameObject.transform.Translate(dx, dy, dz); } }