You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
5.1 KiB
172 lines
5.1 KiB
4 years ago
|
using AX.MessageSystem;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
|
||
|
public class CarAnimationCtrl : MonoBehaviour
|
||
|
{
|
||
|
|
||
|
private Animator anim;
|
||
|
private UnityEngine.AI.NavMeshAgent _NavMeshAgent;
|
||
|
/// <summary>小车是否前进</summary>
|
||
|
private bool isRun = false;
|
||
|
/// <summary>下一个路点</summary>
|
||
|
private Vector3 pathPoint;
|
||
|
/// <summary>寻路路径的所有路点</summary>
|
||
|
public List<Vector3> nowpath;
|
||
|
/// <summary>路点的索引</summary>
|
||
|
public int pathIndex = 0;
|
||
|
public GameObject cube;
|
||
|
|
||
|
/// <summary> 限制车轮转向 </summary>
|
||
|
//private bool wheelLimit =true;
|
||
|
/// <summary>记录出发路点 </summary>
|
||
|
private Vector3 initPoint;
|
||
|
// Use this for initialization
|
||
|
void Awake()
|
||
|
{
|
||
|
anim = GetComponent<Animator>();
|
||
|
_NavMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
|
||
|
nowpath = GetComponent<AgentController>().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<BaseGameObjInfo>().GameObjID)
|
||
|
{
|
||
|
pathIndex = 0;
|
||
|
}
|
||
|
}
|
||
|
public void Down()
|
||
|
{
|
||
|
|
||
|
//var gameObjID = (long)obj.Sender;
|
||
|
//if (gameObjID == GetComponent<BaseGameObjInfo>().GameObjID)
|
||
|
//{
|
||
|
_NavMeshAgent.SetDestination(this.transform.position);
|
||
|
anim.speed = 1;
|
||
|
anim.SetBool("ZJ", true);
|
||
|
//}
|
||
|
}
|
||
|
|
||
|
|
||
|
public void Up()
|
||
|
{
|
||
|
//var gameObjID = (long)obj.Sender;
|
||
|
//if (gameObjID == GetComponent<BaseGameObjInfo>().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);
|
||
|
}
|
||
|
/// <summary>判断方位的值 右边1 左边-1 正对0</summary>
|
||
|
private float crossValue;
|
||
|
/// <summary>判断拐点相对车的方位</summary>
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
/// <summary> 转向修正</summary>
|
||
|
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];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
/// <summary>计算两个坐标是否相同(约等于)</summary>
|
||
|
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;
|
||
|
}
|
||
|
/// <summary>两个数是否约等</summary>
|
||
|
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;
|
||
|
}
|
||
|
}
|