- Unity 5.x Game AI Programming Cookbook
- Jorge Palacios
- 437字
- 2021-07-09 19:37:48
Avoiding agents
In crowd-simulation games, it would be unnatural to see agents behaving entirely like particles in a physics-based system. The goal of this recipe is to create an agent capable of mimicking our peer-evasion movement.
Getting ready
We need to create a tag called Agent and assign it to those game objects that we would like to avoid, and we also need to have the Agent script component attached to them.
An example of how should look the Inspector of a dummy agent to avoid
How to do it...
This recipe will require the creation and handling of just one file:
- Create the
AvoidAgent
behavior, which is composed of a collision avoidance radius and the list of agents to avoid:using UnityEngine; using System.Collections; using System.Collections.Generic; public class AvoidAgent : AgentBehaviour { public float collisionRadius = 0.4f; GameObject[] targets; }
- Implement the
Start
function in order to set the list of agents according to the tag we created earlier:void Start () { targets = GameObject.FindGameObjectsWithTag("Agent"); }
- Define the
GetSteering
function:public override Steering GetSteering() { // body }
- Add the following variables to compute distances and velocities from agents that are nearby:
Steering steering = new Steering(); float shortestTime = Mathf.Infinity; GameObject firstTarget = null; float firstMinSeparation = 0.0f; float firstDistance = 0.0f; Vector3 firstRelativePos = Vector3.zero; Vector3 firstRelativeVel = Vector3.zero;
- Find the closest agent that is prone to collision with the current one:
foreach (GameObject t in targets) { Vector3 relativePos; Agent targetAgent = t.GetComponent<Agent>(); relativePos = t.transform.position - transform.position; Vector3 relativeVel = targetAgent.velocity - agent.velocity; float relativeSpeed = relativeVel.magnitude; float timeToCollision = Vector3.Dot(relativePos, relativeVel); timeToCollision /= relativeSpeed * relativeSpeed * -1; float distance = relativePos.magnitude; float minSeparation = distance - relativeSpeed * timeToCollision; if (minSeparation > 2 * collisionRadius) continue; if (timeToCollision > 0.0f && timeToCollision < shortestTime) { shortestTime = timeToCollision; firstTarget = t; firstMinSeparation = minSeparation; firstRelativePos = relativePos; firstRelativeVel = relativeVel; } }
- If there is one, then get away:
if (firstTarget == null) return steering; if (firstMinSeparation <= 0.0f || firstDistance < 2 * collisionRadius) firstRelativePos = firstTarget.transform.position; else firstRelativePos += firstRelativeVel * shortestTime; firstRelativePos.Normalize(); steering.linear = -firstRelativePos * agent.maxAccel; return steering;
How it works...
Given a list of agents, we take into consideration which one is closest, and if it is close enough, we make it so the agent tries to escape from the expected route of that first one according to its current velocity so that they don't collide.
There's more
This behavior works well when combined with other behaviors using blending techniques (some are included in this chapter); otherwise it's a starting point for your own collision avoidance algorithms.
- 公有云容器化指南:騰訊云TKE實戰(zhàn)與應用
- 我們都是數(shù)據(jù)控:用大數(shù)據(jù)改變商業(yè)、生活和思維方式
- 數(shù)據(jù)庫原理及應用教程(第4版)(微課版)
- Python數(shù)據(jù)分析入門:從數(shù)據(jù)獲取到可視化
- UDK iOS Game Development Beginner's Guide
- 達夢數(shù)據(jù)庫性能優(yōu)化
- 企業(yè)級數(shù)據(jù)與AI項目成功之道
- 跨領(lǐng)域信息交換方法與技術(shù)(第二版)
- Doris實時數(shù)倉實戰(zhàn)
- Mastering ROS for Robotics Programming(Second Edition)
- 量化投資:交易模型開發(fā)與數(shù)據(jù)挖掘
- 數(shù)據(jù)分析方法及應用:基于SPSS和EXCEL環(huán)境
- Redis 6開發(fā)與實戰(zhàn)
- 一本書讀懂區(qū)塊鏈(第2版)
- Hands-On Big Data Analytics with PySpark