- Unity 5.x Game AI Programming Cookbook
- Jorge Palacios
- 447字
- 2021-07-09 19:37:49
Blending behaviors by priority
Sometimes, weighted blending is not enough because heavyweight behaviors dilute the contributions of the lightweights, but those behaviors need to play their part too. That's when priority-based blending comes into play, applying a cascading effect from high-priority to low-priority behaviors.
Getting ready
The approach is very similar to the one used in the previous recipe. We must add a new member variable to our AgentBehaviour
class. We should also refactor the Update
function to incorporate priority
as a parameter to the Agent
class' SetSteering
function. The new AgentBehaviour
class should look something like this:
public class AgentBehaviour : MonoBehaviour { public int priority = 1; // ... everything else stays the same public virtual void Update () { agent.SetSteering(GetSteering(), priority); } }
How to do it...
Now, we need to make some changes to the Agent
class:
- Add a new namespace from the library:
using System.Collections.Generic;
- Add the member variable for the minimum steering value to consider a group of behaviors:
public float priorityThreshold = 0.2f;
- Add the member variable for holding the group of behavior results:
private Dictionary<int, List<Steering>> groups;
- Initialize the variable in the
Start
function:groups = new Dictionary<int, List<Steering>>();
- Modify the
LateUpdate
function so that the steering variable is set by callingGetPrioritySteering
:public virtual void LateUpdate () { // funnelled steering through priorities steering = GetPrioritySteering(); groups.Clear(); // ... the rest of the computations stay the same steering = new Steering(); }
- Modify the
SetSteering
function's signature and definition to store the steering values in their corresponding priority groups:public void SetSteering (Steering steering, int priority) { if (!groups.ContainsKey(priority)) { groups.Add(priority, new List<Steering>()); } groups[priority].Add(steering); }
- Finally, implement the
GetPrioritySteering
function to funnel the steering group:private Steering GetPrioritySteering () { Steering steering = new Steering(); float sqrThreshold = priorityThreshold * priorityThreshold; foreach (List<Steering> group in groups.Values) { steering = new Steering(); foreach (Steering singleSteering in group) { steering.linear += singleSteering.linear; steering.angular += singleSteering.angular; } if (steering.linear.sqrMagnitude > sqrThreshold || Mathf.Abs(steering.angular) > priorityThreshold) { return steering; } }
How it works...
By creating priority groups, we blend behaviors that are common to one another, and the first group in which the steering value exceeds the threshold is selected. Otherwise, steering from the least-priority group is chosen.
There's more...
We could extend this approach by mixing it with weighted blending; in this way, we would have a more robust architecture by getting extra precision on the way the behaviors make an impact on the agent in every priority level:
foreach (Steering singleSteering in group) { steering.linear += singleSteering.linear * weight; steering.angular += singleSteering.angular * weight; }
See also
There is an example of avoiding walls using priority-based blending in this project.
- 同步:秩序如何從混沌中涌現
- MySQL高可用解決方案:從主從復制到InnoDB Cluster架構
- 有趣的二進制:軟件安全與逆向分析
- 數據庫開發實踐案例
- 數據驅動:從方法到實踐
- Sybase數據庫在UNIX、Windows上的實施和管理
- SQL優化最佳實踐:構建高效率Oracle數據庫的方法與技巧
- 圖數據實戰:用圖思維和圖技術解決復雜問題
- 探索新型智庫發展之路:藍迪國際智庫報告·2015(下冊)
- Mastering LOB Development for Silverlight 5:A Case Study in Action
- 聯動Oracle:設計思想、架構實現與AWR報告
- Visual Studio 2013 and .NET 4.5 Expert Cookbook
- Unity 2018 By Example(Second Edition)
- Visual FoxPro數據庫技術基礎
- 云計算寶典:技術與實踐