- Home /
OnTriggerEnter doesn't work with fast animations/movements
Hello developers! I am super beginner, and I have just started learning Unity recently.
For now, I am trying to simulate melee attacks hitting enemy with mesh collider of the weapon.
The reason I want to use mesh collider is only because i prefer super precise hitboxes.
Also I want to use swinging animation speed for attack speed increase/decrease.
The problem is, if the weapon swings too fast(by increasing animation speed), the frames cannot catch it properly, and it looks like the weapon is going through the enemy and won't trigger OnTriggerEnter (it doesn't happen if animation speed is slow enough).
I tried improved the situation by using OnTriggerStay, however, it is still very unreliable on fast animations.
So, I am looking for the best collision detection tricks for this kind of situation.
Please help! Thank you!
P.S. I have designed animation by hand. So no trigonometric motions are used (and I am bad at trigonometrics haha)
,Hello developers! I am super beginner, and I have just started learning Unity recently.
For now, I am trying to simulate melee attacks hitting enemy with mesh collider of the weapon.
The reason I want to use mesh collider is only because i prefer super precise hitboxes.
Also I want to use swinging animation speed for attack speed increase/decrease.
The problem is, if the weapon swings too fast(by increasing animation speed), the frames cannot catch it properly, and it looks like the weapon is going through the enemy and won't trigger OnTriggerEnter (it doesn't happen if animation speed is slow enough).
I tried improved the situation by using OnTriggerStay, however, it is still very unreliable on fast animations.
So, I am looking for the best collision detection tricks for this kind of situation.
Please help! Thank you!
P.S. I have designed animation by hand. So no trigonometric motions are used (and I am bad at trigonometrics haha)
Answer by tomc128 · Sep 10, 2019 at 06:04 PM
There's an option in the Rigidbody component called 'Collision Detection'. Changing this to 'Continuous' can improve fast-moving collisions and triggers. This usually solves the problem for me, but they recently introduced two new continuous modes which may also solve the problem. Here's the documentation for the collision detection: https://docs.unity3d.com/ScriptReference/CollisionDetectionMode.html
Answer by ZeBarba · Sep 10, 2019 at 02:08 PM
You should post your code to receive better answers. Your question text is doubled.
Nevertheless, I had this problem once, so I think I can help a little bit. What I could came up with at the time was this:
Objects moves are not actually continuous, they "teleport" a little bit each frame, according to its speed.
In general, it is enough to detect collisions. If the object is "teleported" inside another object, Unity "pushes" it back outside this object and calls a collision.
But if your speed is large enough, it it miss the object and "teleport" to straight to its back, therefore no collisions will be called.
If you REALLY must use this logic, there a method called SweepTest on the Rigidbody object. You must use it to overcome this limitation. The documentation is here: https://docs.unity3d.com/ScriptReference/Rigidbody.SweepTest.html It basically tracks the trajectory of the object and checks if it would have collided with anything on the way.
Please note that the mesh collider is already expensive, and this will make it even more expensive. Maybe you should consider if that high velocity is actually necessary. Or even the mesh collider.
Your answer
Follow this Question
Related Questions
OnTriggerEnter called one step too late 0 Answers
animation and sound aren't playing on trigger 0 Answers
Could some one help me with this script... 5 Answers
Particle System emits on Trigger Stay, in Unity 5 1 Answer
Trigger area around player. 1 Answer