OnTriggerStay Question

I’m still learning Unity and programming in general so this may be an easy question to answer. I searched the forums and manuals and spent 3 days scratching my head to no avail so here goes:

First a little background. I’m using the book "Programming Game AI by Example (Mat Buckland - great learning tool) and I’m playing around with steering behaviors and converting all the C++ code to Unity JavaScript and taking advantage of Unity’s features.

I have several game agents wandering around on their own, avoiding obstacles and walls and maintaining separation from each other. The physics is being handled by Unity (using forces and rigidbodies for movement) and raycasts for obstacle detection and wall avoidance.

For separation I have a gameController component passing an Array of agents to all the agents so that each agent can figure out where other agent are located so it can maintain separation.

So far so good - all according to the book.

But now I want to change all that to take advantage of Unity’s colliders.

I want to place a sphereCollider around all the agents and give it a neighborhoodRadius value and make the collider a trigger.

My hope is that this one collider will replace all the raycasts for wall detection and obstacle avoidance as well as get rid of the array of agents that every agent has to sort through to find out who is within it’s neighborhood.

Okay, all that aside - here’s my question:

function OnTriggerStay
function OnTriggerEnter
function OnTriggerExit

So far I can successfully use these functions to detect when agents enter/exit/stay within a collider and become a “neighbor.” But I need to pass an array of these neighbors to my steering behaviors.

How can I make an array of the neighbors using OnTriggerStay (collider : Collider)? The array would have to be constantly resized as agents enter and exit the collider. I have been playing around with some code, but getting nowhere.

I hope I am describing this correctly. And again, I am learning to program so if I am not understanding the concept of OnTriggerStay, please let me know.

Thanks for any help.

Mitch

Okay - got it to work. What a difference a night’s sleep can make. I’ll post the code later today after I clean it up.

Unity is amazing.

Did you ever post that code anywhere? Easy to use steering code is what I am sorely lacking skills in. Being an animator more than a programmer, higher math is really tough for me to figure out so some reuseable steering code is something that could really benefit myself and other Unity users.

Dunno what MitchStan ended up doing, but the obvious solution to the issue he had is to maintain an array using OnTriggerEnter and OnTriggerExit, and not worry about OnTriggerStay.

For steering code, perhaps have a look at UnitySteer

Yikes - I think this may have been my first post.

But yes, I wound up using OnTriggerEnter and OnTriggerExit to maintain and array of neighbors.

UnitySteer works great, but I am now trying to roll my own stripped down easy to decode steering behaviors in javascript that will work nicely with my character controller.