Alexis R
Alexis R

Reputation: 61

AI Awareness with Unity3D

I am currently developing an AI system in Unity3D and am wondering if anyone could suggest which of the following awareness models are better?

  1. Use Physics.OverlapSphere at a constant rate of about every 2 seconds to check a range for anything of interest.

  2. Use a Sphere Trigger Collider that is attached to the AI, when an object enters this trigger it starts getting monitored.

Mostly worried about performance vs quality, I have a feeling that model 1 is faster until the AI needs to check everything around it for a particular item, in which case model 2 would return quicker as it already has the collection of local interests. However, will model 2 take up more resources as a trigger collider needs to be sending out checks every physics update?

Upvotes: 2

Views: 536

Answers (1)

N_E
N_E

Reputation: 787

Your 2nd approach is best way to do it, even though,i can easily say that it all depends on your requirements and both have their own uses. (I am "only absolute way of doing things kinda guy")

With Trigger

void OnTriggerEnter(Collider other) you can only get one collision body out of many that have collided. It is faster if you have large collisions and u can definetly expand and contract the radius of your trigger to almost use it as the same effect as overlapping sphere (but not recommended for guaranteed results). As it detects only one collided body its not processor intensive.

With Overlap sphere approach you can

Collider[] OverlapSphere(Vector3 position, float radius, int layerMask, QueryTriggerInteraction queryTriggerInteraction);

you can get collider[] or multiple collision bodies that have collided. This is processor intensive, if you have 50 objects colliding and performance will drop the time you call upon this function.

Another Approach.

For simplicity and proper design of mechanics, Trigger bodies can solve for any complicated scenario. I must advice you, from my experience it is always the perspective you rely upon to solve your problem is the factor that will limit your thoughts and ultimately your results.

Keeping that in mind, I would like you try this approach...create a method that uses

public static float Distance(Vector3 a, Vector3 b);

to give you a boolean answer to detect your actor. It is a very good approach since only a language primitive is streamed (between 2 actors) and not an object and performance of in-built function is just negligible. You can have each enemy actor register themselves (their reference) in the List (to get all actors of interest) inside main actor actor if they are at desired proximity (so yes your enemy checks whether its close than the main actor and tells the actor that he is the one who is very close).

I have tested this approach with 20 other actors having their own scripts running in ipod gen 5, with almost no performance drop.

Upvotes: 1

Related Questions