using System.Collections; using System.Collections.Generic; using UnityEngine; public class move01 : MonoBehaviour { public Animator myanimator; public Rigidbody myrgidbody; // 前進速度 public float forwardSpeed = 0.05f; // 後退速度 public float backwardSpeed = 0.05f; // 旋回速度 public float rotateSpeed = 2.0f; // local velocity Vector3 velocity; // Start is called before the first frame update void Start() { } // 由於它與剛體纏繞在一起,因此需要在FixedUpdate中進行處理。 void FixedUpdate() { float x1 = Input.GetAxis("Horizontal"); float v1 = Input.GetAxis("Vertical"); //設定animator裡面的參數speed = v1 myanimator.SetFloat("speed", v1); //設定animator裡面的參數direction = x1 myanimator.SetFloat("direction", x1); //下面是角色移動處理 //從向上和向下鍵輸入獲取Z軸方向的移動量 velocity = new Vector3(0, 0, v1); // 轉換為角色的局部空間方向 velocity = transform.TransformDirection(velocity); //調整以下v閾值以及Mecanim側的過渡。 if (v1 > 0.1) { velocity *= forwardSpeed; // 乘以運動速度 } else if (v1 < -0.1) { velocity *= backwardSpeed; // 乘以運動速度 } // 按下向上和向下鍵移動角色 transform.localPosition += velocity * Time.fixedDeltaTime/2.0f; //通過按左右鍵在Y軸上旋轉字符 transform.Rotate(0, x1 * rotateSpeed, 0); } }