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; } }
=================================================================================
结束。
相关文章推荐
- 学习-unity3d-游戏开发需要掌握什么技能
- Unity 2D游戏开发教程之2D游戏的运行效果
- Unity 2D游戏开发教程之摄像头追踪功能
- unity编辑器扩展带快捷键使用方法
- Unity3D开发随手笔记
- unity优化
- unity内存管理
- 深入浅出聊Unity3D项目优化:从Draw Calls到GC
- Unity3d之Shader开发介绍
- unity之连接SQLite数据库
- 【unity-Xcode】开发记录---备忘
- Unity中三种截屏方法
- Unity简介
- 对Unity3D 中Quaternion一些整理
- Unity3D协程介绍 以及 使用
- unity编辑器设备丢失
- Unity 如何检测鼠标双击事件
- Unity3d通用工具类之生成文件的MD5
- Unity3d通用工具类之生成文件的MD5
- Unity5内部渲染的优化2:清理