Activating OnTriggerEnter when changing the collider to trigger?
Hello,
I would like to know if it's possible to trigger the OnTriggerEnter event when I change the collider to IsTrigger = true (or another solution).
My situation :
I have a fireball that has a collider (and a rigidBody)
It collide with another object
It change itself to trigger and start to expand
The problem : some objects don't trigger OnTriggerEnter (because they already have collided !)
If I set the fireball collider from trigger from the start, it doesn't detect itself if it collide with something (or is there a way to do that?)
PS : ask if you need more details, I'll try to complete my question
Answer by Naphier · Nov 14, 2016 at 09:12 PM
You'll need to use OnTriggerStay in this case since OnTriggerEnter is only fired on the moment of entry and the objects are already inside the trigger area.
The problem is that OnTriggerStay is called every frame. Hence tracking every fireball individually in every other object that will interact with it. What I wishis a way to "reset" the collision state of my object if it's possible...
But a solution I'm thinking about now is doing a FireAreaDamage Child object that would have the trigger collider, and disable the collider of my parent (could be a solution)
You just put a bool in your class to prevent the OnTriggerStay event from happening more than once. i.e.:
class $$anonymous$$yClass
{
bool doOnce = false;
void OnTriggerStay(Collider other)
{
if (doOnce)
return;
doOnce = true;
// The rest of your code that will only fire once.
}
}
And if you need to reset it, just decide what should do that. It's a very common thing to do in game program$$anonymous$$g.
A simple doOnce boolean won't do it : there is multiple other objects and fireballs , so it imply to store a list ins$$anonymous$$d. I could de that, but I think it's easier in my case to have an AreaDamage children object (that can be also used for other things that do damage in an area)