- Unity 5.x Game AI Programming Cookbook
- Jorge Palacios
- 793字
- 2021-07-09 19:37:48
Following a path
There are times when we need scripted routes, and it's just inconceivable to do this entirely by code. Imagine you're working on a stealth game. Would you code a route for every single guard? This technique will help you build a flexible path system for those situations:
Getting ready
We need to define a custom data type called PathSegment
:
using UnityEngine; using System.Collections; public class PathSegment { public Vector3 a; public Vector3 b; public PathSegment () : this (Vector3.zero, Vector3.zero){} public PathSegment (Vector3 a, Vector3 b) { this.a = a; this.b = b; } }
How to do it...
This is a long recipe that could be seen as a big two-step process. First, we build the Path
class, which abstracts points in the path from their specific spatial representations, and then we build the PathFollower
behavior, which makes use of that abstraction in order to get actual spatial points to follow:
- Create the
Path
class, which consists of nodes and segments but only the nodes are public and assigned manually:using UnityEngine; using System.Collections; using System.Collections.Generic; public class Path : MonoBehaviour { public List<GameObject> nodes; List<PathSegment> segments; }
- Define the
Start
function to set the segments when the scene starts:void Start() { segments = GetSegments(); }
- Define the
GetSegments
function to build the segments from the nodes:public List<PathSegment> GetSegments () { List<PathSegment> segments = new List<PathSegment>(); int i; for (i = 0; i < nodes.Count - 1; i++) { Vector3 src = nodes[i].transform.position; Vector3 dst = nodes[i+1].transform.position; PathSegment segment = new PathSegment(src, dst); segments.Add(segment); } return segments; }
- Define the first function for abstraction, called
GetParam
:public float GetParam(Vector3 position, float lastParam) { // body }
- We need to find out which segment the agent is closest to:
float param = 0f; PathSegment currentSegment = null; float tempParam = 0f; foreach (PathSegment ps in segments) { tempParam += Vector3.Distance(ps.a, ps.b); if (lastParam <= tempParam) { currentSegment = ps; break; } } if (currentSegment == null) return 0f;
- Given the current position, we need to work out the direction to go to:
Vector3 currPos = position - currentSegment.a; Vector3 segmentDirection = currentSegment.b - currentSegment.a; segmentDirection.Normalize();
- Find the point in the segment using vector projection:
Vector3 pointInSegment = Vector3.Project(currPos, segmentDirection);
- Finally,
GetParam
returns the next position to go to along the path:param = tempParam - Vector3.Distance(currentSegment.a, currentSegment.b); param += pointInSegment.magnitude; return param;
- Define the
GetPosition
function:public Vector3 GetPosition(float param) { // body }
- Given the current location along the path, we find the corresponding segment:
Vector3 position = Vector3.zero; PathSegment currentSegment = null; float tempParam = 0f; foreach (PathSegment ps in segments) { tempParam += Vector3.Distance(ps.a, ps.b); if (param <= tempParam) { currentSegment = ps; break; } } if (currentSegment == null) return Vector3.zero;
- Finally,
GetPosition
converts the parameter as a spatial point and returns it:Vector3 segmentDirection = currentSegment.b - currentSegment.a; segmentDirection.Normalize(); tempParam -= Vector3.Distance(currentSegment.a, currentSegment.b); tempParam = param - tempParam; position = currentSegment.a + segmentDirection * tempParam; return position;
- Create the
PathFollower
behavior, which derives fromSeek
(remember to set the order of execution):using UnityEngine; using System.Collections; public class PathFollower : Seek { public Path path; public float pathOffset = 0.0f; float currentParam; }
- Implement the
Awake
function to set the target:public override void Awake() { base.Awake(); target = new GameObject(); currentParam = 0f; }
- The final step is to define the
GetSteering
function, which relies on the abstraction created by thePath
class to set the target position and applySeek
:public override Steering GetSteering() { currentParam = path.GetParam(transform.position, currentParam); float targetParam = currentParam + pathOffset; target.transform.position = path.GetPosition(targetParam); return base.GetSteering(); }
How it works...
We use the Path
class in order to have a movement guideline. It is the cornerstone, because it relies on GetParam
to map an offset point to follow in its internal guideline, and it also uses GetPosition
to convert that referential point to a position in the three-dimensional space along the segments.
The path-following algorithm just makes use of the path's functions in order to get a new position, update the target, and apply the Seek
behavior.
There's more...
It's important to take into account the order in which the nodes are linked in the Inspector for the path to work as expected. A practical way to achieve this is to manually name the nodes with a reference number.
An example of a path set up in the Inspector window
Also, we could define the OnDrawGizmos
function in order to have a better visual reference of the path:
void OnDrawGizmos () { Vector3 direction; Color tmp = Gizmos.color; Gizmos.color = Color.magenta;//example color int i; for (i = 0; i < nodes.Count - 1; i++) { Vector3 src = nodes[i].transform.position; Vector3 dst = nodes[i+1].transform.position; direction = dst - src; Gizmos.DrawRay(src, direction); } Gizmos.color = tmp; }
- Hands-On Data Structures and Algorithms with Rust
- SQL Server入門經典
- 正則表達式必知必會
- 企業大數據系統構建實戰:技術、架構、實施與應用
- 數據革命:大數據價值實現方法、技術與案例
- SQL優化最佳實踐:構建高效率Oracle數據庫的方法與技巧
- 達夢數據庫運維實戰
- TextMate How-to
- 探索新型智庫發展之路:藍迪國際智庫報告·2015(下冊)
- Power BI智能數據分析與可視化從入門到精通
- Mastering LOB Development for Silverlight 5:A Case Study in Action
- 活用數據:驅動業務的數據分析實戰
- 數據庫與數據處理:Access 2010實現
- Visual FoxPro數據庫技術基礎
- SIEMENS數控技術應用工程師:SINUMERIK 840D-810D數控系統功能應用與維修調整教程