- Home /
Object moving too fast, so that the collider does not work (C#)
Hi! I just found out a problem with my game, I've created an enemy that "fly's by" the screen in order to hurt the player, but the problem is that the enemy moves to fast so that the collider doesn't interact.
How can I fix this so that the enemy will take damage to the player no matter how fast it is going? Or well how fast it is currently going?
Answer by Dave-Carlile · Apr 20, 2016 at 12:27 PM
You can set the collision detection mode to one of the Continuous values. One thing to note from the documentation:
This has a big impact on physics performance,
The physics engine must do a lot more math for this so use it judiciously.
I did some tests on this a while back. The descriptions of the Collision modes are a little deceptive and dont actually help much with the speed problem beyond a certain amount.
The docs talk about which modes to use in which situations but in actual fact they are designated for particular scenarios and arent really meant for improving detection for fast movers, much.
The testing I performed drew the conclusion that the only way to detect collisions beyond a certain speed was to use a $$anonymous$$esh Collider as it was the only Collider capable of keeping up. The difference was huge.
http://answers.unity3d.com/questions/900306/rigid-body-continuous-and-continuousdynamic-seem-t.html
Seems like a lot of the comments have been deleted in that thread! Sorry.
I'd suggest to forget about the collision modes, you'll never make them 100% reliable for fast-moving objects. Ins$$anonymous$$d, raycast from the last frame's position towards the current position. This will ensure that all colliders between both positions will be caught. You could also use Physics.SphereCast etc.
There sould be a sample script implementing this method somewhere on the net, possibly on the Unity user wiki.
Answer by gamedevelopmenttsunami · Apr 30, 2019 at 02:33 PM
What @cherno says is probably the best solution. There was a deep discussion a few years ago about how discrete and continuous detection have almost no difference in accuracy, when detecting fast moving objects. So don't bother.
I'd suggest to forget about the collision modes, you'll never make them 100% reliable for fast-moving objects. Instead, raycast from the last frame's position towards the current position. This will ensure that all colliders between both positions will be caught. You could also use Physics.SphereCast etc. There sould be a sample script implementing this method somewhere on the net, possibly on the Unity user wiki.
Your answer
Follow this Question
Related Questions
Fist Punch Collision 0 Answers
Collider collision processing speed 2 Answers
Can an object to transfer to another as it increases your speed? 1 Answer
Picked up objects passes through objects 2 Answers