using UnityEngine; using System.Collections; //Author: ZCG /// /// 小车的动画控制 /// public class CarAnimatorControl : MonoBehaviour { /// 小车Animator private Animator anim; /// 当前的小车 private GameObject car; /// 当前小车的nav private UnityEngine.AI.NavMeshAgent _NavMeshAgent; private LineamentEvent _LineamentEvent; private void Start() { car = this.gameObject; anim = GetComponent(); _NavMeshAgent = GetComponent(); nowpath = GetComponent().nowpath; _LineamentEvent = GetComponent(); #if UNITY_EDITOR_WIN try { cube = GameObject.Find("Cube");//路点所用的cube } catch (System.Exception) { } #endif } private void Update() { if (gameObject.tag == "Carother") { return; } if (!_NavMeshAgent.enabled) { return; } if (_NavMeshAgent.velocity.magnitude > 0) { speed = 1; } else { speed = 0; }//anim.SetFloat("WS", Input.GetAxisRaw("Vertical")); if (speed > 0) isRun = true; else isRun = false; AnmatorCpntrol(); if (speed>0) { // AgentPath(); AnimLimit(); RightOrLeft(); //if (Vector3.Distance(this.transform.position, initPoint) > 1) //{ // print("走出当前路点"); // anim.SetFloat("AD", 0); //} } } #region 控制小车运动动画 /// 小车是否前进 private bool isRun = false; /// 小车速度 public float speed = 0; /// 动画控制 private void AnmatorCpntrol() { if(isRun) anim.SetFloat("WS", 1); else anim.SetFloat("WS", 0); if (isRun /*&& wheelLimit*/) anim.SetFloat("AD", RightOrLeft()); else anim.SetFloat("AD", 0); } /// 判断方位的值 右边1 左边-1 正对0 private float crossValue; /// 判断拐点相对车的方位 private float RightOrLeft() { Vector3 vectorTarget = /*targetGameObject.transform.position*//*_NavMeshAgent.steeringTarget*//*_NavMeshAgent.nextPosition*/ pathPoint - transform.position; vectorTarget = new Vector3(vectorTarget.x, 0, vectorTarget.z); Vector3 vectorForward = transform.forward; float dotValue = Vector3.Dot(vectorForward.normalized, vectorTarget.normalized); //float angle = Mathf.Acos(dotValue) * Mathf.Rad2Deg; //Vector3 crossValue = Vector3.Cross(vectorForward, vectorTarget); 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 Vector3 pathPoint; /// 寻路路径的所有路点 public Vector3[] nowpath; /// 路点的索引 public int pathIndex = 0; public GameObject cube; /// 绘制所有路点 private void DrawPath() { #if UNITY_EDITOR_WIN //绘制所有路点 if (cube != null && pathIndex == 0 && nowpath.Length>0) { for (int i = 0; i < nowpath.Length; i++) { Instantiate(cube).transform.position = nowpath[i]; //用cube表示路点 } } #endif } /// 限制车轮转向 //private bool wheelLimit =true; /// 记录出发路点 private Vector3 initPoint; /// 转向修正 private void AnimLimit() { DrawPath(); if (nowpath==null) { Debug.Log("消防车发生异常移动"); return; } if (nowpath.Length > 0 && pathIndex == 0)//如果开始寻路初始化下一路点 { pathPoint = nowpath[0]; } if (pathIndex < nowpath.Length-1 && Vector3.Distance(transform.position, pathPoint) < 0.05)//如果到达路点 索引+1 { initPoint = nowpath[pathIndex]; pathIndex++; pathPoint = nowpath[pathIndex]; // print("到达路点"); // print("下一个路点索引" + pathIndex); } if (nowpath.Length > 0 && pathIndex 3 )//小车行驶过当前路点 { if (!AboutCount(transform.forward, (pathPoint - transform.position).normalized))//判断小车朝向下一路点是否正确 { pathIndex++; pathPoint = nowpath[pathIndex]; // print("纠正"); } } //print(nowpath.Length); //print(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; } #endregion }