I am doing bullet penetration and was wondering how I could achieve this. I just want to raycast to the point that I hit a wall then do another raycast from that end point to see the distance the bullet travels inside the wall. Can anyone enlighten me?
Thanks – Michael
Check out Physics.RaycastAll. Instead of stopping the raycast at the first thing hit, it continues to go through everything hit, in the whole scene. Using this, it is possible to sort the list of hits by distance, go through the sorted list in order, and decide at what point the bullet has run out of energy.
Note that this will only give hits for where the bullet enters, not exits. To get a list of exits, it’ll take another RaycastAll in the negative direction. After that, though, it should be possible to match up entrances to exits, and get the length of the vector from an entrance to an exit to figure out how far it traveled inside a particular collider.
Well you could use RaycastHit then take the point.origin to see the location of entry. The thing is that the ray will only go until it hits one collider.
To go through multiple I believe you have to use RaycastAll: Unity - Scripting API: Physics.RaycastAll
It will cast a ray and return all the hits. You can store the first hit as “impactorigin” then the second hit as “impact length”
The problem with this is that for the second to be trigger youd need a collider within the wall which would be predefined, meaning itd be hard to do this dynamically without any further coding and messing with the inside collider?
If youre using different calibers of a bullet, you can probably do this:
Make wall with box collider. Then make an Empty Gameobject with another box collider (box because its less intensive to compute unlike mesh? Or is it the other way around, i forget). Then parent that Object to the Wall itself. Ensure the empty gameobject is within the wall.
Now upon a script you can do like a bool on the wall
OnCollission then GetComponentFromChildren() then change position of the inside collider where closer to the wall surface is for smaller calibers (hence low penetrations) and the opposite for larger calibers.
And thus you can do the following:
RaycastHitDist - RaycastHitOrigin = the bullet travel distance
Im about to leave right now so I cant give you the exact code or test it out myself anytime soon (out for the weekend)