I’m in need of a way add objects that are inside of a trigger to a collection without repeating them.
In the docs it says “OnTriggerStay is called once per frame for every Collider other that is touching the trigger.”
So I could add the object inside the trigger to the collection this way and since it’s executed once for every object it would add all objects inside the trigger to the collection. The problem is that I want to clear the array before I start adding duplicates to the collection but I don’t know where to do this.
Ideally I would like to use on OnTriggerStay to add all objects to the collection, perform a function on the whole array, like moving them and such and then clear the array and then refill it with the object inside the trigger for the next time I perform such function.
I hope I was able to explain it well, Anyone know a way to do this?
Aren’t collision reported in arrays if you get them from the object with the trigger?
So wait, you want a list of objects that are inside a trigger? Try:
OnTriggerEnter - Add to array
OnTriggerExit - Remove from array
I gave the OnTriggerEnter and OnTriggerExit aproach some thought it could work this way, but I’ll be testing against all objects inside the trigger this script would be attached to, so wouldn’t I have to loop trough all objects in the array to know which object to remove? I guess I’m just confused here because the aproach makes soo much sence.
Well, you add them all to the array as they enter, checking each time with a for loop to add them to the first open spot. It’s the same with the exit. You find the one that == it, and remove it.
This only gets processor intensive if you have hundreds of these passing in and out constantly.
I’m not much of a programmer - still learning - but this works for me and I think it does what you are asking:
#pragma strict
// connect to appropriate components via inspector
var hockeyPlayer2 : HockeyPlayer2;
var sphereCollider : SphereCollider;
// expose these variables
var speed : float;
var entityCount : int;
var entities : Transform[];
var regionFactor : float;
function Awake()
{
sphereCollider.isTrigger = true; // set this in the inspector, redundant check here
regionFactor = Random.Range(.25, 1.25);
entityCount = 0;
}
function FixedUpdate()
{
speed = transform.parent.rigidbody.velocity.magnitude;
sphereCollider.radius = speed*regionFactor;
}
function OnTriggerEnter (entity : Collider)
{
if (entity.tag == "Body") // just check for the body collider in player
{
entityCount++; // increment count since we have a PlayerGO entering the collider
if (entityCount == 1) // no previous neighbors
entities = new Transform[entityCount]; // resize the array
else
{
var oldEntities : Transform[] = entities; // save the previous neighbors array in oldNeighbors
entities = new Transform[entityCount]; // resize the neighbors array
var i : int = 0; // initialize the index
for (var oldEntity : Transform in oldEntities) // iterate through the oldNeighbors array
{
entities[i] = oldEntity; // assign the oldNeighbors to neighbors
i ++; // increment the index
}
}
var newEntity : Transform = entity.transform; // get the new hockeyPlayer who has entered the collider
entities[entityCount-1] = newEntity; // add it to the array
}
}
function OnTriggerExit (entity : Collider)
{
if (entity.tag == "Body") // just check for the body collider in player
{
entityCount--; // decrement count since we have a PlayerGO exiting the collider
if (entityCount) // if there are neighbors
{
var oldEntities : Transform[] = entities; // save the previous neighbors array in oldNeighbors
entities = new Transform[entityCount]; // resize the neighbors array
var i : int = 0; // initialize the index
var exEntity : Transform = entity.transform; // get hockeyPlayer who has exited the collider
for (var oldEntity : Transform in oldEntities) // iterate through the oldNeighbors array
{
if (exEntity != oldEntity) // is this oldNeighbor the exNeighbor?
{
entities[i] = oldEntity; // if not, assign this old neighbor to neighbors[index]
i++; // increment index
}
} // exNeighbor has been removed from neighbors array!
}
else
entities = new Transform[0];
}
}
Thanks to all the good advice from you all it works now, I was kinda hoping for a way to do it without the for loop but this works and it’s true that it only gets intensive if I have a lot of intersecting objects which shouldn’t be too bad in my case.
Thanks all