天津23维预案
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.
 
 
 
 
 
 

191 lines
5.8 KiB

using UnityEngine;
using System.Collections;
//Author: ZCG
/// <summary>
/// 小车的动画控制
/// </summary>
public class CarAnimatorControl : MonoBehaviour
{
/// <summary>小车Animator</summary>
private Animator anim;
/// <summary>当前的小车</summary>
private GameObject car;
/// <summary>当前小车的nav</summary>
private UnityEngine.AI.NavMeshAgent _NavMeshAgent;
private LineamentEvent _LineamentEvent;
private void Start()
{
car = this.gameObject;
anim = GetComponent<Animator>();
_NavMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
nowpath = GetComponent<AgentControl>().nowpath;
_LineamentEvent = GetComponent<LineamentEvent>();
#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 控制小车运动动画
/// <summary>小车是否前进</summary>
private bool isRun = false;
/// <summary>小车速度</summary>
public float speed = 0;
/// <summary>动画控制</summary>
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);
}
/// <summary>判断方位的值 右边1 左边-1 正对0</summary>
private float crossValue;
/// <summary>判断拐点相对车的方位</summary>
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;
}
/// <summary>下一个路点</summary>
private Vector3 pathPoint;
/// <summary>寻路路径的所有路点</summary>
public Vector3[] nowpath;
/// <summary>路点的索引</summary>
public int pathIndex = 0;
public GameObject cube;
/// <summary>绘制所有路点</summary>
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
}
/// <summary> 限制车轮转向 </summary>
//private bool wheelLimit =true;
/// <summary>记录出发路点 </summary>
private Vector3 initPoint;
/// <summary> 转向修正</summary>
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<nowpath.Length -1 && Vector3.Distance(transform.position, initPoint) > 3 )//小车行驶过当前路点
{
if (!AboutCount(transform.forward, (pathPoint - transform.position).normalized))//判断小车朝向下一路点是否正确
{
pathIndex++;
pathPoint = nowpath[pathIndex];
// print("纠正");
}
}
//print(nowpath.Length);
//print(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;
}
#endregion
}