unity的自动寻路之 —— wayPoint寻路的实现方式

unity的自动寻路之 —— wayPoint寻路的实现方式孙广东 2015 6 28

孙广东 2015.6.28

看了看  Unity的官方案例,就顺便看了 wayPoint相关。

效果:

unity的自动寻路之 ------ wayPoint寻路的实现方式

WaypointProgressTracker.cs  【固定】

unity的自动寻路之 ------ wayPoint寻路的实现方式

using System; using System.Collections; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif namespace UnityStandardAssets.Utility { public class WaypointCircuit : MonoBehaviour { public WaypointList waypointList = new WaypointList(); [SerializeField] private bool smoothRoute = true; private int numPoints; private Vector3[] points; private float[] distances; public float editorVisualisationSubsteps = 100; public float Length { get; private set; } public Transform[] Waypoints { get { return waypointList.items; } } // 这是在这里会保存 GC 分配 private int p0n; private int p1n; private int p2n; private int p3n; private float i; private Vector3 P0; private Vector3 P1; private Vector3 P2; private Vector3 P3; // Use this for initialization private void Awake() { if (Waypoints.Length > 1) { CachePositionsAndDistances(); } numPoints = Waypoints.Length; } public RoutePoint GetRoutePoint(float dist) { // 位置和方向 Vector3 p1 = GetRoutePosition(dist); Vector3 p2 = GetRoutePosition(dist + 0.1f); Vector3 delta = p2 - p1; return new RoutePoint(p1, delta.normalized); } public Vector3 GetRoutePosition(float dist) { int point = 0; if (Length == 0) { Length = distances[distances.Length - 1]; } dist = Mathf.Repeat(dist, Length); while (distances[point] < dist) { ++point; } // get nearest two points, ensuring points wrap-around start & end of circuit // 得到最近的两个点,确保点环绕电路的开始与结束 p1n = ((point - 1) + numPoints)%numPoints; p2n = point; // found point numbers, now find interpolation value between the two middle points // 发现点的数目,现在找到中间两点之间内的一个插值 i = Mathf.InverseLerp(distances[p1n], distances[p2n], dist); if (smoothRoute) { // 有关两点之间的光滑catmull-rom计算。 // 获取周围的 2 点的指数,因为catmull-rom 函数要求四个点 p0n = ((point - 2) + numPoints)%numPoints; p3n = (point + 1)%numPoints; // 第二个点可能已经是最后的 'last' 点了 - a dupe of the first, // (to give a value of max track distance instead of zero) // but now it must be wrapped back to zero if that was the case. p2n = p2n%numPoints; P0 = points[p0n]; P1 = points[p1n]; P2 = points[p2n]; P3 = points[p3n]; return CatmullRom(P0, P1, P2, P3, i); } else { // 两个点之间的简单线性插值: p1n = ((point - 1) + numPoints)%numPoints; p2n = point; return Vector3.Lerp(points[p1n], points[p2n], i); } } private Vector3 CatmullRom(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float i) { // 这是 catmull-rom 方程 // Un-magic this, lord vector! return 0.5f* ((2*p1) + (-p0 + p2)*i + (2*p0 - 5*p1 + 4*p2 - p3)*i*i + (-p0 + 3*p1 - 3*p2 + p3)*i*i*i); } private void CachePositionsAndDistances() { // transfer the position of each point and distances between points to arrays for // speed of lookup at runtime // 在运行时查找到阵列速度点之间传输的每个点和距离的位置 points = new Vector3[Waypoints.Length + 1]; distances = new float[Waypoints.Length + 1]; float accumulateDistance = 0; for (int i = 0; i < points.Length; ++i) { var t1 = Waypoints[(i)%Waypoints.Length]; var t2 = Waypoints[(i + 1)%Waypoints.Length]; if (t1 != null && t2 != null) { Vector3 p1 = t1.position; Vector3 p2 = t2.position; points[i] = Waypoints[i%Waypoints.Length].position; distances[i] = accumulateDistance; accumulateDistance += (p1 - p2).magnitude; } } } private void OnDrawGizmos() { DrawGizmos(false); } private void OnDrawGizmosSelected() { DrawGizmos(true); } private void DrawGizmos(bool selected) { waypointList.circuit = this; if (Waypoints.Length > 1) { numPoints = Waypoints.Length; CachePositionsAndDistances(); Length = distances[distances.Length - 1]; Gizmos.color = selected ? Color.yellow : new Color(1, 1, 0, 0.5f); Vector3 prev = Waypoints[0].position; if (smoothRoute) { for (float dist = 0; dist < Length; dist += Length/editorVisualisationSubsteps) { Vector3 next = GetRoutePosition(dist + 1); Gizmos.DrawLine(prev, next); prev = next; } Gizmos.DrawLine(prev, Waypoints[0].position); } else { for (int n = 0; n < Waypoints.Length; ++n) { Vector3 next = Waypoints[(n + 1)%Waypoints.Length].position; Gizmos.DrawLine(prev, next); prev = next; } } } } [Serializable] public class WaypointList { public WaypointCircuit circuit; public Transform[] items = new Transform[0]; } public struct RoutePoint { public Vector3 position; public Vector3 direction; public RoutePoint(Vector3 position, Vector3 direction) { this.position = position; this.direction = direction; } } } } /// 编辑器对 Inspector面板的定制 namespace UnityStandardAssets.Utility.Inspector { #if UNITY_EDITOR [CustomPropertyDrawer(typeof (WaypointCircuit.WaypointList))] public class WaypointListDrawer : PropertyDrawer { private float lineHeight = 18; private float spacing = 4; public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); float x = position.x; float y = position.y; float inspectorWidth = position.width; // 绘制 label // 不要缩进的子字段 var indent = EditorGUI.indentLevel; EditorGUI.indentLevel = 0; var items = property.FindPropertyRelative("items"); var titles = new string[] {"Transform", "", "", ""}; var props = new string[] {"transform", "^", "v", "-"}; var widths = new float[] {.7f, .1f, .1f, .1f}; float lineHeight = 18; bool changedLength = false; if (items.arraySize > 0) { for (int i = -1; i < items.arraySize; ++i) { var item = items.GetArrayElementAtIndex(i); float rowX = x; for (int n = 0; n < props.Length; ++n) { float w = widths[n]*inspectorWidth; // Calculate rects // 计算 rects Rect rect = new Rect(rowX, y, w, lineHeight); rowX += w; if (i == -1) { EditorGUI.LabelField(rect, titles[n]); } else { if (n == 0) { EditorGUI.ObjectField(rect, item.objectReferenceValue, typeof (Transform), true); } else { if (GUI.Button(rect, props[n])) { switch (props[n]) { case "-": items.DeleteArrayElementAtIndex(i); items.DeleteArrayElementAtIndex(i); changedLength = true; break; case "v": if (i > 0) { items.MoveArrayElement(i, i + 1); } break; case "^": if (i < items.arraySize - 1) { items.MoveArrayElement(i, i - 1); } break; } } } } } y += lineHeight + spacing; if (changedLength) { break; } } } else { // add button // 添加"+"按钮 var addButtonRect = new Rect((x + position.width) - widths[widths.Length - 1]*inspectorWidth, y, widths[widths.Length - 1]*inspectorWidth, lineHeight); if (GUI.Button(addButtonRect, "+")) { items.InsertArrayElementAtIndex(items.arraySize); } y += lineHeight + spacing; } // add all button // 添加所有按钮 var addAllButtonRect = new Rect(x, y, inspectorWidth, lineHeight); if (GUI.Button(addAllButtonRect, "Assign using all child objects")) { var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit; var children = new Transform[circuit.transform.childCount]; int n = 0; foreach (Transform child in circuit.transform) { children[n++] = child; } Array.Sort(children, new TransformNameComparer()); circuit.waypointList.items = new Transform[children.Length]; for (n = 0; n < children.Length; ++n) { circuit.waypointList.items[n] = children[n]; } } y += lineHeight + spacing; // rename all button // 重命名所有按钮 var renameButtonRect = new Rect(x, y, inspectorWidth, lineHeight); if (GUI.Button(renameButtonRect, "Auto Rename numerically from this order")) { var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit; int n = 0; foreach (Transform child in circuit.waypointList.items) { child.name = "Waypoint " + (n++).ToString("000"); } } y += lineHeight + spacing; // Set indent back to what it was // 设置缩进 EditorGUI.indentLevel = indent; EditorGUI.EndProperty(); } public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { SerializedProperty items = property.FindPropertyRelative("items"); float lineAndSpace = lineHeight + spacing; return 40 + (items.arraySize*lineAndSpace) + lineAndSpace; } // comparer for check distances in ray cast hits // 比较器检查射线发射命中的距离 public class TransformNameComparer : IComparer { public int Compare(object x, object y) { return ((Transform) x).name.CompareTo(((Transform) y).name); } } } #endif } 

unity的自动寻路之 ------ wayPoint寻路的实现方式

using System; using UnityEngine; namespace UnityStandardAssets.Utility { public class WaypointProgressTracker : MonoBehaviour { // 此脚本可以用于任何对象,支持沿着一条路线为标志的waypoints。 // 此脚本管理向前看沿路线的数量,并跟踪进度和圈数。 [SerializeField] private WaypointCircuit circuit; // 一个引用关于 waypoint-based 我们应该沿着的路线 [SerializeField] private float lookAheadForTargetOffset = 5; // 我们将目标,沿着路线的偏移 [SerializeField] private float lookAheadForTargetFactor = .1f; // 一个倍数,增加目标与沿着线路的距离,基于当前速度 [SerializeField] private float lookAheadForSpeedOffset = 10; // 前面的偏移的唯一路线速度调整(作为waypoint目标的rotation变换) [SerializeField] private float lookAheadForSpeedFactor = .2f; // 一个倍数,沿着路线调整速度添加距离 [SerializeField] private ProgressStyle progressStyle = ProgressStyle.SmoothAlongRoute; // 进度样式:是否smoothly更新位置沿着路线(好的曲线路径)或只是正常到达每个航点。 [SerializeField] private float pointToPointThreshold = 4; // 接近waypoint的阈值,一旦达到这个值,目标将切换到下一个目标地点:只用于PointToPoint模式。 public enum ProgressStyle { SmoothAlongRoute, PointToPoint, } // 这些是public,由其他对象读取。让一个AI知道在哪里头! public WaypointCircuit.RoutePoint targetPoint { get; private set; } public WaypointCircuit.RoutePoint speedPoint { get; private set; } public WaypointCircuit.RoutePoint progressPoint { get; private set; } public Transform target; private float progressDistance; // 圆形(环形)路线的进展,平滑smooth模式中使用。 private int progressNum; // 当前waypoint数,点对点point-to-point模式中使用。 private Vector3 lastPosition; // 用于计算当前速度(因为我们可能没有一个刚体组件) private float speed; // 此对象的当前速度(从最后一帧的delta计算) // 设置脚本属性 private void Start() { // 我们使用transform表示目标点,这个点被认为是为即将到来的变化的速度点。这允许此信息传递给 AI 而无需进一步依赖此组件。您可以手动创建transform设置该组件 *and* AI,然后此组件将更新它,和 AI 可以阅读它。 if (target == null) { target = new GameObject(name + " Waypoint Target").transform; } Reset(); } // 对象重置为合理的值 public void Reset() { progressDistance = 0; progressNum = 0; if (progressStyle == ProgressStyle.PointToPoint) { target.position = circuit.Waypoints[progressNum].position; target.rotation = circuit.Waypoints[progressNum].rotation; } } private void Update() { if (progressStyle == ProgressStyle.SmoothAlongRoute) { // 确定我们目前目标的位置 (这是不同于当前的进展位置,它是一个确定值沿着前方路线的) 我们使用 lerp 作为一种随着时间的推移速度进行平滑处理的简单方式。 if (Time.deltaTime > 0) { speed = Mathf.Lerp(speed, (lastPosition - transform.position).magnitude/Time.deltaTime, Time.deltaTime); } target.position = circuit.GetRoutePoint(progressDistance + lookAheadForTargetOffset + lookAheadForTargetFactor*speed) .position; target.rotation = Quaternion.LookRotation( circuit.GetRoutePoint(progressDistance + lookAheadForSpeedOffset + lookAheadForSpeedFactor*speed) .direction); // 得到我们的当前路线的进展 progressPoint = circuit.GetRoutePoint(progressDistance); Vector3 progressDelta = progressPoint.position - transform.position; if (Vector3.Dot(progressDelta, progressPoint.direction) < 0) { progressDistance += progressDelta.magnitude*0.5f; } lastPosition = transform.position; } else { // 点对点模式。 如果我们足够近,只是增加Waypoint: Vector3 targetDelta = target.position - transform.position; if (targetDelta.magnitude < pointToPointThreshold) { progressNum = (progressNum + 1)%circuit.Waypoints.Length; } target.position = circuit.Waypoints[progressNum].position; target.rotation = circuit.Waypoints[progressNum].rotation; // 得到我们的当前路线的进展 progressPoint = circuit.GetRoutePoint(progressDistance); Vector3 progressDelta = progressPoint.position - transform.position; if (Vector3.Dot(progressDelta, progressPoint.direction) < 0) { progressDistance += progressDelta.magnitude; } lastPosition = transform.position; } } private void OnDrawGizmos() { if (Application.isPlaying) { Gizmos.color = Color.green; Gizmos.DrawLine(transform.position, target.position); Gizmos.DrawWireSphere(circuit.GetRoutePosition(progressDistance), 1); Gizmos.color = Color.yellow; Gizmos.DrawLine(target.position, target.position + target.forward); } } } } 

最后: 看看Unity5的 sample中的 Car 和 飞机 的AI案例中:

unity的自动寻路之 ------ wayPoint寻路的实现方式

unity的自动寻路之 ------ wayPoint寻路的实现方式

unity的自动寻路之 ------ wayPoint寻路的实现方式

unity的自动寻路之 ------ wayPoint寻路的实现方式











版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/222390.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午3:52
下一篇 2026年3月17日 下午3:53


相关推荐

  • groupadd mysql_Linux命令之groupadd

    groupadd mysql_Linux命令之groupaddgroupadd 选项 组创建一个新的组 Groupadd 命令使用命令行中指定的值加上系统默认值创建新的组账户 新组将根据需要输入系统 1 选项 f force 如果指定的组已经存在 此选项将失明了仅以成功状态退出 当与 g 一起使用 并且指定的 GID MIN 已经存在时 选择另一个唯一的 GID 即 g 关闭 g gidGID 这个值必须是唯一的 除非使用 o 选项 但必须是非负的

    2026年3月19日
    2
  • Navicat 12 安装激活教程

    Navicat 12 安装激活教程试了很多 10 和 11 的激活成功教程 均卡在了激活码上 没想到这个 12 的版本试验成功了 感谢原文作者的分享 这里只是个转载 软件 工具 教程均在下面的压缩包里链接 https pan baidu com s 1CBdWNamJeZG ojZ9w nbsp 提取码 d7g2 说下其他的几个点 1 尽量安装在非系统盘 2 教程中有提到 VS2017 这应该不是安装激活成功教程的必要条件 而是使用某些功

    2026年3月18日
    2
  • COLORREF、COLOR、RGB转化总结分析及在VC++中的使用

    COLORREF、COLOR、RGB转化总结分析及在VC++中的使用COLORREF COLOR RGB 转化总结分析及在 VC 中的使用真彩色是指显示出来的图像颜色与真实世界中颜色非常自然逼真 使得人眼难以区分它们之间的差别 通常使用 RGB 图像颜色表示法来表现真彩色图像 即用 3 字节来表示一个真彩色像素的颜色值 Windows 采用该方法来表现颜色 其中 SDK 提供一个名为 RGB 的宏来将不同的 R G B 颜色值转化为 24 位的颜色值 其原型如下 COLORRERR

    2026年3月18日
    3
  • c# 进度条的使用(例子)[通俗易懂]

    c# 进度条的使用(例子)[通俗易懂]2012-05-2320:21在用c#做WinFrom开发的过程中。我们经常需要用到进度条(ProgressBar)用于显示进度信息。这时候我们可能就需要用到多线程,如果不采用多线程控制进度条,窗口很容易假死(无法适时看到进度信息)。下面我就简单结合一个我写的例子给大家做一个介绍。首先看一下程序界面第一步:设计界面不说了…注意需要引用usingSyst

    2025年6月12日
    5
  • xshell连接不上虚拟机Linux

    xshell连接不上虚拟机Linux平台说明 win10 VMware12Pro LinuxCentOS7 Xshell5 问题描述 在 Linux 学习过程中 重装了 VMware 出现了 Xshell 无法连接 Linux 虚拟机的问题解决过程 期间尝试了不少方法 关闭并禁用防火墙 查看 ssh 状态并开启 ssh 服务 检查并开启 win 平台 VMware 相关服务 在 Linux 系统中修改了网络配置文件 ifcfg ens33 固定了 IP 地址 依然无法连接 最后在 VMware 中重置网络设置 成功解决 步骤 重置完

    2026年3月17日
    2
  • 线程池ThreadPoolExecutor简介[通俗易懂]

    线程池ThreadPoolExecutor简介[通俗易懂]1前言线程池是并发编程中一个重要的概念和技术。大多数异步或并发执行任务都会用到线程池。线程池,正如其名,它是有一定数量的线程的池子,它会执行被提交过来的任务,执行完一个任务后不会马上结束,它们会

    2022年7月4日
    21

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号