状态机中的人物状态
一人物惯性移动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; } }二人物刹车
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2477510.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!