I’m trying to code a script that simulates ballistic penetration through objects. I’m using raycasts to detect collisions and I have a surface script for surface weights on objects I want to collider with. I have almost everything working. I’m able to correctly detect contact points and determine if a ballistic should penetrate a surface based on the surface’s weight and the ballistic’s force (applied at contact point). I also have ricochets and damage built in to the class as well (irelevant to this problem though).
What I need is to be able to determine an “exit” point for the ballistic after penetrating (an opposite point on the mesh). Mesh geometry calculations is not one of my strengths, so I’m at a loss of how to determine this. I also need to calculate the exit point to be able to implement object width as part of my ballistic penetration model (see image below).
If anyone can help me out, that would be great. Thanks in advance!

extend the ray beyond the bounds of the penetrated object, then cast backwards.
That would work except my script will have no way of knowing how far ahead to extend the ray. Am I able to extend the ray by infinity?
Just pick a fairly big number that makes sense for your scene size. Cast forward once, then back from there to the origin. If you use Physics.RaycastAll and get hits 5 from the first cast, the second cast should give you 5 as well. The first hit from the original cast minus the 5th hit from the second should give you that distance. The only thing you have to make sure of is that you don’t cast from inside an object and that you have no single sided objects like planes or you won’t get the same number of hits from both casts. If that happens you don’t know which match up with which.
Hopefully that was clear enough.
No, not by infinity, but I think ‘A very long way’ would work. ‘A very long way’ could be double the biggest dimension of any model you have. Granted, this is not super robust, but it’s a good place to start.
So, find a point a ‘very long way’ along your incoming ray.
Then do a ray cast from this point in the reverse direciton of your ray, using Mathf.Infinity or just ‘a very long way’ again.
Cast against only the collider of interest using Collider.Raycast.
Alright thanks, I’ll give that a try. It does sound like its gonna be a lot more intensive than I want this calculation to be. I have some weapons that shoot at a sustained rate of 30+ rounds per second, and I have a feeling this method is gonna have a huge performance hit because of all the extra raycasts. I guess I won’t know for sure until I implement this.
Is there any way to use mesh data to calculate the exit point? Like OppositePointOnBounds() or something?
Ok so I implemented it:
//offset point to backcast from
float backcastOffset = 200;
//Only use the layer that we previously collided with
LayerMask layer = 1 << ballistic.HitPos().collider.gameObject.layer;
//Backcast from the offset point
RaycastHit[] hits;
hits = Physics.RaycastAll(ballistic.HitPos().point +
ballistic.transform.TransformDirection(Vector3.Normalize(ballistic.velocity) * backcastOffset),
ballistic.transform.TransformDirection(Vector3.Normalize(ballistic.velocity) * -1), backcastOffset, layer);
//hits from backcast
foreach(RaycastHit hit in hits){
//Point on the same collider that is the exit point for penetration
if(hit.collider.gameObject == ballistic.HitPos().collider.gameObject){
ballistic.transform.position = hit.point;
}//end if
}//end foreach
I did notice quite a bit of lag from just one ballistic colliding with a surface. I still have to add a costly square root calculation to determine the width of the object between the contact and exit points.
Dont do a RaycastAll on the backcast. Just cast against the collider(s) of interest using Collider.Raycast
Hah, I had no idea that function existed. Now it runs a lot better. Thanks!