您的位置:首页 > 移动开发 > Unity3D

Unity Game Programming AI(3)路径跟随

2015-09-06 01:09 501 查看

Unity Game Programming AI(3)路径跟随

路径跟随在游戏AI中是经常用到的,像塔防游戏中,敌人迎着固定路线移动,就是用的路径跟随。

下面,一个简单的示例代码,实现这一功能。

我们用Path.cs定义路径:

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);
            }
        }
    }
}


用VehicleFollowing.cs实现这一功能:

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;
    }
}




=================================================================================

结束。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: