Movement causing infinite loops in OnTriggerEnter and OnTriggerExit.
Help, I'm stuck and it's been two days.
I want my AI units to go attack a stationary enemy, then move onto the next until none are left. I already find the closest enemy... but what's the best way to move my units to them? If you can't tell I'm new and looking for best practices.
The reason I ask is because I followed a tower defense tutorial but I'm having infinite loops of OnTriggerEnter and OnTriggerExit when my unit reaches and "stands" (maybe it never really stops?) next to the enemy tower. I am currently moving my units via their mesh nav agent with some function I can't remember atm, after baking the scene.
I've figured out that Discrete collision creates the loop issue, but Continuous collision (set on my unit) doesn't. I also have no idea why this fixes it and am afraid of relying on it because it seems like a band-aid for my lack of proper movement and collision knowledge. I thought continuous collision detection was for fast moving objects. Theses units are walking into the towers slowly.
I've been troubleshooting weird behavior when I'd rather just move and and do this the "right" way if anyone has suggestions.
Answer by TheyLeftMe4Dead · Feb 24, 2017 at 04:01 AM
From what you've said, I think the root of your problem is that your AI units need to know how far away they are from the target tower. Brakeys has an excellent Tower Defense game tutorial series on youtube about this exact problem (Only reverse, from towers to enemies, but it's the same difference). But long story short, you need to check the distance between the AI units and the tower and stop them once they reach said distance.
Note: To find the distance between two GameObjects, you need to subtract their position Vectors.
Your answer
Follow this Question
Related Questions
Cart objects clips with rigidbody objects 0 Answers
Offset NavMeshAgent Destination 0 Answers
Occasional NavMeshAgent gets stuck and goes the wrong direction 1 Answer
Need some example C# Syntax 0 Answers