using AX.MessageSystem; using System.Collections; using System.Collections.Generic; using UnityEngine; public class CarAnimationCtrl : MonoBehaviour { private Animator anim; private UnityEngine.AI.NavMeshAgent _NavMeshAgent; /// 小车是否前进 private bool isRun = false; /// 下一个路点 private Vector3 pathPoint; /// 寻路路径的所有路点 public List nowpath; /// 路点的索引 public int pathIndex = 0; public GameObject cube; /// 限制车轮转向 //private bool wheelLimit =true; /// 记录出发路点 private Vector3 initPoint; // Use this for initialization void Awake() { anim = GetComponent(); _NavMeshAgent = GetComponent(); nowpath = GetComponent().corners; //MessageDispatcher.AddListener("PackUpTheLiftArm", Up); //MessageDispatcher.AddListener("FixedSupport", Down); MessageDispatcher.AddListener("SetpathIndex", PathFinding); } private void OnDestroy() { //MessageDispatcher.RemoveListener("PackUpTheLiftArm", Up); //MessageDispatcher.RemoveListener("FixedSupport", Down); MessageDispatcher.RemoveListener("SetpathIndex", PathFinding); } void PathFinding(IMessage obj) { if ((long)obj.Sender == GetComponent().GameObjID) { pathIndex = 0; } } public void Down() { //var gameObjID = (long)obj.Sender; //if (gameObjID == GetComponent().GameObjID) //{ _NavMeshAgent.SetDestination(this.transform.position); anim.speed = 1; anim.SetBool("ZJ", true); //} } public void Up() { //var gameObjID = (long)obj.Sender; //if (gameObjID == GetComponent().GameObjID) //{ anim.speed = 5; anim.SetBool("ZJ", false); //} } public void AnimStopEvent() { anim.speed = 0; anim.SetBool("ZJ", false); } private void Update() { if (!_NavMeshAgent.enabled) { return; } if (_NavMeshAgent.velocity.magnitude > 0) { isRun = true; anim.speed = 1; AnimLimit(); } else { isRun = false; } forward(isRun); } public void forward(bool isRun) { if (isRun) anim.SetFloat("WS", 1); else anim.SetFloat("WS", 0); if (isRun) anim.SetFloat("AD", RightOrLeft()); else anim.SetFloat("AD", 0); } /// 判断方位的值 右边1 左边-1 正对0 private float crossValue; /// 判断拐点相对车的方位 private float RightOrLeft() { Vector3 vectorTarget = pathPoint - transform.position; vectorTarget = new Vector3(vectorTarget.x, 0, vectorTarget.z); Vector3 vectorForward = transform.forward; float dotValue = Vector3.Dot(vectorForward.normalized, vectorTarget.normalized); crossValue = Vector3.Dot(vectorTarget.normalized, transform.right); if (crossValue > 0.05)//&& !_LineamentEvent.ControlJuBi return 1; else if (crossValue < -0.05)//&& !_LineamentEvent.ControlJuBi return -1; else return 0; } /// 转向修正 private void AnimLimit() { if (nowpath == null) { Debug.Log("消防车发生异常移动"); return; } if (nowpath.Count > 0 && pathIndex == 0)//如果开始寻路初始化下一路点 { pathPoint = nowpath[0]; } if (pathIndex < nowpath.Count - 1 && Vector3.Distance(transform.position, pathPoint) < 0.05)//如果到达路点 索引+1 { initPoint = nowpath[pathIndex]; pathIndex++; pathPoint = nowpath[pathIndex]; } if (nowpath.Count > 0 && pathIndex < nowpath.Count - 1 && Vector3.Distance(transform.position, initPoint) > 3)//小车行驶过当前路点 { if (!AboutCount(transform.forward, (pathPoint - transform.position).normalized))//判断小车朝向下一路点是否正确 { pathIndex++; pathPoint = nowpath[pathIndex]; } } } /// 计算两个坐标是否相同(约等于) private bool AboutCount(Vector3 a, Vector3 b) { if (IsEquality(a.x, b.x) && IsEquality(a.y, b.y) && IsEquality(a.z, b.z)) return true; else return false; } /// 两个数是否约等 private bool IsEquality(float x, float y) { if (x > y && x - y < 0.05) return true; else if (x <= y && y - x < 0.05) return true; else return false; } }