I’ve been looking around for a solution to this problem for over a month now, but nothing seems to work properly and I’m reaching my wit’s end…
I’m learning Unity by making a top down 2d shooter, and the projectile in the game moves fast. I’ve read that when an object moves fast, to detect collision trigger it’s best to enable Continuous Collision Detection to avoid the projectile “teleporting” past it’s target. However even after I’ve enable it, it’s still happens when a projectile fly at another collider at specific angles
Here are the details:
Projectile:
Moves with AddForce, has both RigidBody2d and BoxCollider2d (with is trigger enabled). Body type is Dynamic, has continuous collision detection
Target:
Moves with AddForce also, has both RigidBody2d and BoxCollider2d (is trigger disabled). Body type is Dynamic, has discrete collision detection (I’ve also tried setting it to continuous, but no dice)
Both object has scale (1,1,1), the sprite dimensions is 200x200 in pixels (not sure if this affect anything, so I added this just in case).
The problem doesn’t happens if I change Fixed Timestep from 0.02 to 0.01, but I would rather not impact the game performance by forcing the physic engine to update 200 times per sec…
Any help or suggestions is appreciated, and thank you very much in advance for any help you can offer
Its best to to not use traditional collision with high speed objects at all.
In my opinion, the best tried and true method is to instead, keep track of your previous position and run a ray cast from prev to current position. You can always ensure you will know if you have ‘passed’ through something.
Thank you very much for your quick reply. I did thought that maybe I should use ray casting but I thought that might cost more computing than collision (I’m guessing that’s not true?). I’ll try it and see if it I can get it right
In that case should I stop using physic on the projectile all together and just use transform.Translate instead?
Thats actually exactly how my space ships fire projectiles. Things are just to fast for traditional collision. Not to mention the projectiles moving at 1000 mps, but they have to also have the ships speed added into their start speed as the ship can be doing 3600 mps. Just some loose avg low ball parked numbers, but you get the idea. The projectiles have have no colliders, and no rigdibodies. They only care about other things colliders. The ray cast never misses.
In fact, its also how the ship calculates collisions, because again, just too fast for standard contact collision.
Just to point out the obvious here too. with these kind of speeds and distances, physics can get pretty terrible due to unitys inability to use anything other than a float for point precision. So, that leads to the next obvious go to. Origin Management. But thats another story.
This isn’t always incorrect. Continuous collision detection in 2D physics (Box2D) is rock solid. The only time this fails is when the Transform is used to reposition a body because then it isn’t actually moving. Also, Continuous doesn’t work with Triggers.
That said, Box2D is certainly optimized for more realistic sizes/speed. 1000mps or three times the speed of sound is getting a little fast. 4600 is getting very silly for using game physics though.
I’m not so much saying don’t use queries because for projectiles, this is often better anyway because you’re not interested in stopping at the point of contact but rather knowing where it’d hit so a circle, capsule or raycast is often better anyway.
Fair points, that said, i cant speak for 2d anything, i am exclusively in 3d spaces. Thanks for the info.
Some of the values are indeed extreme, however, this has been compensated for with systems in place that always allow for ‘large’ numbers , that play out inside very ‘small’ unity spaces.
Continuous not working with trigger would actually explain a lot why it wasn’t working. Either way, I’ve managed to get Raycast working after a bit of tweaking around thanks to Homicide suggestion.
The speed of the projectile is not as extreme as 1000mps in my case (the clipping starts happening at around 60mps or more, and the fastest projectile only goes to 100), but since it’s a projectile and I only care about where it’d hit, do you think Raycast would be the better option in this case?
I have a question out of curiosity, why doesn’t Unity support continuous triggers?
I don’t see any technical limitation, and there is a demand for it in attack detection, for example.
It is misleading to ignore the inspector setting and switch internally to discrete mode.