状态机中的人物状态

张开发
2026/4/4 4:33:20 15 分钟阅读

分享文章

状态机中的人物状态
一人物惯性移动using System.Collections; using System.Collections.Generic; using UnityEngine; public class CharMove3 : MonoBehaviour { public Transform charTrans; //角色坐标 public Vector3 currentVelocity; //当前速度 public float maxSpeed; //最大速率 public float turnSpeed; //转向速率 public float acceleration; //加速度 public float deceleration; //衰减速度 void Update() { float horizontal Input.GetAxis(Horizontal); //AD float vertical Input.GetAxis(Vertical); //WS Vector3 direction new Vector3(horizontal, 0f, vertical).normalized; if (direction.sqrMagnitude 0) //如果有输入 { //算当前速度在目标方向上的投影 float directionSpeed Vector3.Dot(direction, currentVelocity); //惯性速度 Vector3 remainSpeed currentVelocity - directionSpeed * direction; //加速 directionSpeed acceleration * Time.deltaTime; //限速 directionSpeed Mathf.Clamp(directionSpeed, -maxSpeed, maxSpeed); //转向速度减速 remainSpeed Vector3.MoveTowards(remainSpeed, Vector3.zero, turnSpeed * Time.deltaTime); currentVelocity directionSpeed * direction remainSpeed; } else { currentVelocity Vector3.MoveTowards(currentVelocity,Vector3.zero,deceleration * Time.deltaTime); } charTrans.position currentVelocity*Time.deltaTime; } }二人物刹车

更多文章