- Unity 5.x Game AI Programming Cookbook
- Jorge Palacios
- 369字
- 2021-07-09 19:37:47
Pursuing and evading
Pursuing and evading are great behaviors to start with because they rely on the most basic behaviors and extend their functionality by predicting the target's next step.
Getting ready
We need a couple of basic behaviors called Seek
and Flee
; place them right after the Agent
class in the scripts' execution order.
The following is the code for the Seek
behaviour:
using UnityEngine; using System.Collections; public class Seek : AgentBehaviour { public override Steering GetSteering() { Steering steering = new Steering(); steering.linear = target.transform.position - transform.position; steering.linear.Normalize(); steering.linear = steering.linear * agent.maxAccel; return steering; } }
Also, we need to implement the Flee
behavior:
using UnityEngine; using System.Collections; public class Flee : AgentBehaviour { public override Steering GetSteering() { Steering steering = new Steering(); steering.linear = transform.position - target.transform.position; steering.linear.Normalize(); steering.linear = steering.linear * agent.maxAccel; return steering; } }
How to do it...
Pursue
and Evade
are essentially the same algorithm but differ in terms of the base class they derive from:
- Create the
Pursue
class, derived fromSeek
, and add the attributes for the prediction:using UnityEngine; using System.Collections; public class Pursue : Seek { public float maxPrediction; private GameObject targetAux; private Agent targetAgent; }
- Implement the
Awake
function in order to set up everything according to the real target:public override void Awake() { base.Awake(); targetAgent = target.GetComponent<Agent>(); targetAux = target; target = new GameObject(); }
- As well as implement the
OnDestroy
function, to properly handle the internal object:void OnDestroy () { Destroy(targetAux); }
- Finally, implement the
GetSteering
function:public override Steering GetSteering() { Vector3 direction = targetAux.transform.position - transform.position; float distance = direction.magnitude; float speed = agent.velocity.magnitude; float prediction; if (speed <= distance / maxPrediction) prediction = maxPrediction; else prediction = distance / speed; target.transform.position = targetAux.transform.position; target.transform.position += targetAgent.velocity * prediction; return base.GetSteering(); }
- To create the
Evade
behavior, the procedure is just the same, but it takes into account thatFlee
is the parent class:public class Evade : Flee { // everything stays the same }
How it works...
These behaviors rely on Seek
and Flee
and take into consideration the target's velocity in order to predict where it will go next; they aim at that position using an internal extra object.
推薦閱讀
- Access 2016數(shù)據(jù)庫(kù)教程(微課版·第2版)
- 數(shù)據(jù)之巔:數(shù)據(jù)的本質(zhì)與未來(lái)
- 信息系統(tǒng)與數(shù)據(jù)科學(xué)
- Spark核心技術(shù)與高級(jí)應(yīng)用
- 達(dá)夢(mèng)數(shù)據(jù)庫(kù)性能優(yōu)化
- PostgreSQL指南:內(nèi)幕探索
- 一本書(shū)講透Elasticsearch:原理、進(jìn)階與工程實(shí)踐
- SAS金融數(shù)據(jù)挖掘與建模:系統(tǒng)方法與案例解析
- Python數(shù)據(jù)分析從小白到專家
- Oracle 11g+ASP.NET數(shù)據(jù)庫(kù)系統(tǒng)開(kāi)發(fā)案例教程
- Internet of Things with Python
- 云工作時(shí)代:科技進(jìn)化必將帶來(lái)的新工作方式
- SOLIDWORKS 2018中文版機(jī)械設(shè)計(jì)基礎(chǔ)與實(shí)例教程
- MySQL核心技術(shù)手冊(cè)
- C# 7 and .NET Core 2.0 High Performance