- Home /
Trigger events sometimes not working disregarding frame rate
Hello, everyone! I have a trigger attached to character hand to act as an attack box, and recently noticed the OnTriggerEnter sometimes not reacting to enemy colliders. Already tried most popular solutions: increased fixed timestamp (up to 0.001), added rigidbody (with Continuous Dynamic collision detection), and reduced animation speed - still no signs of improvement. Even tried to replace trigger with OverlapSphere, but result is the same, even when I did OverlapSphere check with "InvokeRepeating" with repeat rate 0.001s. Colliders are not small - 1 in radius and 10 in height for attack box and 2 and 8 for an enemy, so it's hard to believe that the issue's in size. Do I still miss something, or it's some common unity issue I did not hear about?
how are you moving the colliders? physics get updated using fixedtimestep frequence. the invokerepeating and overlapsphere make no sense remove it
They're attached to skeleton bones: whether character performs attack, trigger moves along with the bone. The enemy collider is static.
use the physics debugger to see if the collider are moving correctly
Answer by tfalmeida91 · Oct 15, 2021 at 04:01 PM
Have you tried attatching a RigidBody with Continuous detection mode (Not continuous dynamic) to the objects that are supposed to be hit by the sword?
I have once fiddled with this issue and found out that I could put a bullet (or many) travelling at the speed of light (exagerating a bit but you get the ideia) and it still detected the collision, but setting the rigidbody on the bullet as Continuous Dynamic, and objects that slow or static, with Continuous. It worked flawlessly.
Edit: If the fast moving object is being moved by an Animator and not a Rigidbody, make sure you set the Update Mode of the Animator to "Animate Physics"
Good idea, but result was the same. Can't tell exactly if it became better (as this issue is very random) but I'm trying to completely get rid of it:)
Oh, another VERY important thing, if you want the collisions to happed, then the movement of the fast moving object AKA the sword, must intirely rely on the physics, for example, I moved my bullets with rigidbody.addforce
If it's an animator, try checking for AnimatePhysics
YES, this is it! At least I was not encountering this problem for 3 days straight. Thank you very much!
Answer by SamElTerrible · Oct 15, 2021 at 01:12 PM
I had a similar issue in the past when adding a collider to a swinging sword to deal damage. I think collision detection in this case is not fully trustworthy; and trying to patch it didn't really help.
Instead, I'd suggest you send an event at a specific point (or several points) during your attack animation through code. If an enemy is within range when this event is sent, then they'll take damage.
This way you don't need colliders and will get more predictable results.
Thanks, that's an option, but I'd rather leave it as a last resort, because, first, it won't be cool for user experience to make hits at certain animation points, second, putting lots of animaiton event even for one animation would be a pain, and I have about ten more to fix. Maybe if I add this solution to current implementation it'd make up for missing hits, but I first want to find out if there's an easier solution:)