using UnityEngine; public class PlayerContrller1 : MonoBehaviour { //字段 private Transform mmTransform; private Rigidbody mmRigidbody; //屬性 //開始事件 Awake(),Start() void Start () {
//獲取自身 Transform組件和Rigidbody組件的引用 mmTransform=gameObject.GetComponent<Transform>(); mmRigidbody=gameObject.GetComponent<Rigidbody>(); } //更新事件,Update(),FixUpdate void Update () { PlayerMove(); } //方法 private void PlayerMove() {
//使用系統預設的w,a,s,d 控制Cube移動 float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); Vector3 dir = new Vector3(h, 0, v); //剛體移動的特點:物體的位置+方向,太快就方向*一個小數,使之慢一點 mmRigidbody.MovePosition(mmTransform.position + dir * 0.2f); } }
Input輸入鍵設置:
上方菜單欄:edit=>Project settings=>input