- Home /
OnTrigger events not working
I have great count of enemies in my game and they all try to reach target. if they collide with target they stop mooving and start attacking target. When target escapes reach of enemy by escaping attack range trigger, enemie should react with OnTriggerExit. But the problem is when there is a lot of enemies they are pushing enemies that are attacking to side so they are not close to target, and in that moment OnTriggerExit just not happening. As result enemies are standing and attacking in distance to target. I tried to set their rigidbodies sleep mode to never sleep and collision detection to continious but it is not solving the problem. How can i work this out? Please help!
Answer by Legend_Bacon · Feb 13, 2018 at 03:27 PM
Hello there,
Rather than relying on collision, in your case I think it would make a lot of sense to use distance instead. For example, if X is your "contact" distance:
While the distance is above X, move towards target.
While the distance is equal or lower than X, stop moving and attack once.
Once attack is over, check again from the first step.
~
In Unity, you can do that easily with Vector3.Distance().
Hope that helps!
Cheers,
~LegendBacon
I fixed my problem, just wrote some crutches. I can not make it your way because i have more than 100 enemies in scene at the same time, and this game is for android. With collisions they are not perfomance heavy. When they all will calculate distances at least few times per second it will be a bit laggy i think. Also i can not do things without because i made them through collision understand if they are pushing mates that are attacking now, and if so they go around ti find some space. But anyway thanks, i'm getting replies veryyyy rarely.