转自:胡胜博客http://blog.csdn.net/husheng0/article/details/48233651
把路径封装成一个对象:
using UnityEngine;
using System.Collections;
public class Path: MonoBehaviour
{
public bool isDebug = true;
public float Radius = 2.0f;
public Vector3[] pointA; //导航点
public float Length
{
get
{
return pointA.Length; //返回数组长度
}
}
public Vector3 GetPoint(int index)
{
return pointA[index]; //返回导航数字
}
/// <summary>
/// Show Debug Grids and obstacles inside the editor
/// </summary>
void OnDrawGizmos()
{
if (!isDebug)
return;
for (int i = 0; i < pointA.Length; i++)
{
if (i + 1 < pointA.Length)
{
Debug.DrawLine(pointA[i], pointA[i + 1], Color.red);
}
}
}
}
这个脚本让谁移动添就加到谁身上
using UnityEngine;
using System.Collections;
public class VehicleFollowing : MonoBehaviour
{
public Path path;
public float speed = 20.0f; //速度
public float mass = 5.0f; //质量
public bool isLooping = true; //是否循环运动
//Actual speed of the vehicle
private float curSpeed;
private int curPathIndex; //开始时所在的导航点
private float pathLength; //整个路径的长度
private Vector3 targetPoint; //下一个导航点
Vector3 velocity;
// Use this for initialization
void Start ()
{
pathLength = path.Length;
curPathIndex = 0;
//get the current velocity of the vehicle
velocity = transform.forward;
}
// Update is called once per frame
void Update ()
{
//Unify the speed
curSpeed = speed * Time.deltaTime;
targetPoint = path.GetPoint(curPathIndex);
//If reach the radius within the path then move to next point in the path
if(Vector3.Distance(transform.position, targetPoint) < path.Radius)
{
//Don't move the vehicle if path is finished
if (curPathIndex < pathLength - 1) //判断是否到了终点
curPathIndex ++;
else if (isLooping)
curPathIndex = 0;
else
return;
}
//Move the vehicle until the end point is reached in the path
if (curPathIndex >= pathLength )
return;
//Calculate the next Velocity towards the path
if(curPathIndex >= pathLength - 1 && !isLooping)
velocity += Steer(targetPoint, true); //Steer计算加速度
else
velocity += Steer(targetPoint);
transform.position += velocity; //Move the vehicle according to the velocity
transform.rotation = Quaternion.LookRotation(velocity); //Rotate the vehicle towards the desired Velocity
}
//Steering algorithm to steer the vector towards the target
public Vector3 Steer(Vector3 target, bool bFinalPoint = false)
{
//Calculate the directional vector from the current position towards the target point
Vector3 desiredVelocity = (target - transform.position);
float dist = desiredVelocity.magnitude;
//Normalise the desired Velocity
desiredVelocity.Normalize();
//Calculate the velocity according to the speed
if (bFinalPoint && dist < 10.0f)
desiredVelocity *= (curSpeed * (dist / 10.0f));
else
desiredVelocity *= curSpeed;
//Calculate the force Vector
Vector3 steeringForce = desiredVelocity - velocity;
Vector3 acceleration = steeringForce / mass;
return acceleration;
}
}