AI 车移动

using UnityEngine;
using System.Collections;

public class AIMoving : MonoBehaviour {

//	public GameObject DirObj;
	public GameObject BodyObj;

	private Transform _transform;
	// Use this for initialization
	void Start () {
		_transform = transform;
	}
	
	// Update is called once per frame
	void Update () {
		move ();
	}

	private Vector3 mTmpSimpleVector1 = new Vector3(0,0,0);
	private void move() {

//		update_target_point ();
		updatePoint ();

		if (currentSpeed < 0)
			currentSpeed = 0.00001f;
		// 计算移动方向
		calcMoveDircetion();

//		mTmpSimpleVector1 = _transform.forward;
//
//		float distance = currentSpeed*Time.deltaTime;
//
//		mTmpSimpleVector1 = mTmpSimpleVector1 * distance;

		_transform.Translate (Vector3.forward*currentSpeed*Time.deltaTime);

		calcNpcEulerAngles ();
	}


	static  float PLAYER_TURN_INTERPOLATION_BASE = 250.0f;

	public int m_point_id = 0;
	Vector3 moveVector=new Vector3(0,0,0);

	private Quaternion mCurr = new Quaternion();
	
	private Quaternion mDest = new Quaternion();
	        Quaternion mResult = new Quaternion();
	private Vector3 mAjdustDir = new Vector3(0,0,0);

	public  float currentSpeed=50f;

	private Vector3 mZAxisTemp = new Vector3(0,0,0);

	private void calcMoveDircetion() {

		Vector3 nextPoint = get_point_pos (m_point_id);

		moveVector = nextPoint - _transform.position;

		moveVector = new Vector3 (moveVector.x,moveVector.y,moveVector.z);

//		Debug.Log ("nextPoint:"+nextPoint);
//		Debug.Log ("moveVector:"+moveVector);
//		moveVector =_transform.position - nextPoint;

//		DirObj.transform.localEulerAngles = moveVector;
//		DirObj.transform.forward = moveVector;
	//	moveVector = moveVector.Normalize ();
		moveVector.Normalize ();
//		Debug.Log ("moveVector:"+moveVector);


		mZAxisTemp = _transform.forward;
		mZAxisTemp.Normalize ();
//		Debug.Log ("mZAxisTemp:"+mZAxisTemp);

		float radian111 = Vector3.Angle (moveVector, mZAxisTemp);
//		Debug.Log ("radian111:"+radian111);


		mCurr.Set (mZAxisTemp.x,mZAxisTemp.y,mZAxisTemp.z,0);
		mDest.Set (moveVector.x,moveVector.y,moveVector.z,0);



//		float interpolation = PLAYER_TURN_INTERPOLATION_BASE * 800
//			/  currentSpeed;
		float interpolation = currentSpeed / 100 ;
		float a = Time.deltaTime / interpolation;

//		Debug.Log ("Time.deltaTime:"+Time.deltaTime);

		a = Mathf.Clamp (a, 0, 1);
	//	a = 0.03f;
		Debug.Log ("a:"+a);

		mResult=Quaternion.Slerp(mCurr, mDest, a);
		mAjdustDir.Set(mResult.x, mResult.y, mResult.z);

//		Debug.Log ("mAjdustDir:"+mAjdustDir+"  mZAxisTemp:"+mZAxisTemp+"  moveVector:"+moveVector);

		float radian22 = Vector3.Angle (mAjdustDir, mZAxisTemp);
//		Debug.Log ("radian22:"+radian22);








	//	_transform.LookAt(nextPoint);

//		Quaternion aa = Quaternion.Euler (mAjdustDir);

//		_transform.rotation = Quaternion.Euler (new Vector3(0,radian*0.1f,0));
//		Debug.Log ("aa:"+aa);
	//	_transform.rotation = aa;

	//	if(Mathf.Abs(radian) >10){
//			_transform.Rotate(new Vector3(0,radian*0.1f,0));
//			_transform.rotation = Quaternion.AngleAxis(radian*0.1f, Vector3.up);
	//	}

	//	_transform.rotation = Quaternion.LookRotation(mAjdustDir, Vector3.up);
//		_transform.rotation = Quaternion.AngleAxis(mAjdustDir.y, Vector3.up);
//		_transform.Rotate (mAjdustDir);
	}


	void update_target_point()
	{
		Vector3 _pos_0 = get_point_pos(m_point_id) - _transform.position;
		Vector3 _pos_1 = get_point_pos(m_point_id + 1) - _transform.position;		

//		Debug.Log ("0:"+_pos_0.magnitude+"  1:"+_pos_1.magnitude);

		if(_pos_0.magnitude > _pos_1.magnitude)
		{
			m_point_id ++;
			if(A_autodrome.GetInstance().m_points.Count <= m_point_id)
			{
				m_point_id = 0;
	//			m_count ++;
			}			
		}


	}

	void updatePoint(){


		float distance;
		do {
			Vector3 _pos_0 = get_point_pos(m_point_id) - _transform.position;
			distance = _pos_0.x * _pos_0.x
				+ _pos_0.y * _pos_0.y + _pos_0.z
					* _pos_0.z;

//			Debug.Log("distance:"+distance);
			if (distance < 50) {
				m_point_id ++;
				if(A_autodrome.GetInstance().m_points.Count <= m_point_id)
				{
					m_point_id = 0;
					//			m_count ++;
				}	
//				increace();
			} else {
				break;
			}
		} while (true);



	}



	Vector3 get_point_pos(int id)
	{
		if(A_autodrome.GetInstance().m_points.Count <= id)
		{
			id = 0;
		}
		
		return  ((GameObject)A_autodrome.GetInstance().m_points[id]).transform.position;
	}

	private void calcNpcEulerAngles() {

		Vector3 calcCross = Vector3.Cross(mZAxisTemp,mAjdustDir);

		int flag = -1;
		if (calcCross.y > 0) {
			flag = 1;
		}
		// 弧度:飞行器的z轴向量与飞行器到下一个路点的向量的夹角
//		float radian = mAjdustDir.calcAngle(mZAxisTemp) * flag;
		float radian = Vector3.Angle(mAjdustDir, mZAxisTemp)* flag;
	//	float radian = Vector3.Angle (moveVector, mZAxisTemp)*flag;;
//		Debug.Log ("radian:"+radian);
//		_transform.eulerAngles.y += radian;
		_transform.eulerAngles = new Vector3 (_transform.eulerAngles.x,_transform.eulerAngles.y+radian,_transform.eulerAngles.z);
		// ZLog.d("gold", "euler y: " + mModels[index].eulerAngles.y);
//		BodyObj.transform.localEulerAngles= new Vector3 (BodyObj.transform.eulerAngles.x,BodyObj.transform.eulerAngles.y,BodyObj.transform.eulerAngles.z+radian);
		BodyObj.transform.localRotation = Quaternion.Euler (new Vector3(0,0,radian*5f));
	}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值