- Home /
Raycast, OnCollisionExit - most efficient way to get outcome or exit point on Collider?
The thing is, I want fast(!) Rigidbody(!) bullets (so, drag and gravity included) to come through Colliders in certain circumstances, and give me info of entry and outcome points, like collision.contacts OnCollisionEnter, to carefully place decals or something. Using just OnCollision is simple by disabling bullet Collider or IgnoreCollision for a millisecond, switch attempt stance, and rereflect velocity, but gets to unconsistent results, like displacement of contacts. It's easy to get income point by just raycasting ahead, but I can't get right outcome point even with Queries.hitBackfaces... but this is too expensive anyways. I'll try to combine these next time. But maybe I'm just reinventing the bicycle? Any suggestions? P.S.: Still can't believe this function is not implemented by default...
Answer by Servail · Dec 20, 2017 at 12:45 AM
So, I'm just realized bullets can be triggers and receive OnTrigger calls. I've added another trigger Collider and now i can predict collision or model my own, based on penetration, in example. Raycasting forward/backwards OnTriggerEnter is probably the best method for precise determining entry/exit points (so as thickness). UPD: Whoops... If velocity is too high (like 1000), trigger can't do the trick. It's just come through (ofc I'm using continuous dynamic) until you enable collider, but then it can't predict collision enough much ahead, and collision called (so does trigger, lol). You need to extend trigger, and this results in huge performance drops and unsuspected behavior (so it extends along transform.forward, not velocity). Raycasting ahead every FixedUpdate is much better and efficient, and already gives hit point. So, I'm just was trying to reinvent the bicycle? But raycasting every FixedUpdate for many bullets is not much of a deal... I still will be trying to find solution in one-two calls.
By Physics.Raycast towards bullet velocity (entry point), then collider.Raycast against ray.direction on some point (which is maxThickness) on that ray (exit point).