- Home /
How to use a Raycast to see the distance traveled within a collider.
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? :D Thanks -- Michael
Answer by zach-r-d · Jun 27, 2015 at 02:17 PM
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.
Oh youve beat me to the raycastall, but you forgot to mention you need two colliders others the second wont trigger a RaycastHit deter$$anonymous$$e the lengths
RaycastHighWithinWall - RaycastHitImpactOrigin = distance traveled within wall
Two colliders are not necessary. As I mention in the second paragraph, perfor$$anonymous$$g a second RaycastAll in the negative direction of the first will find exit points without having to make separate colliders.
I agree but I think a box collider is more efficient than a second unnecessary raycast, correct?
I'm not sure I understand what you're saying; perfor$$anonymous$$g the second negative raycast works with any kind of collider. If you're suggesting having two colliders per wall, remember that perfor$$anonymous$$g one raycast on double the number of objects will be about the same as perfor$$anonymous$$g two raycasts on the original number of objects in terms of efficiency.
Perfect, thanks! Works like a charm I actually got this idea as well in the long time unity takes to accept a question. Although I didnt use raycast all because the game has bullet drop as well so from tha point it penetrates some of the stats of the bullet drop. It goes through its target then it will raycast again instantly to see if it tavels through another target.
Answer by adriandevera · Jun 27, 2015 at 02:27 PM
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: http://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html 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)
Your answer
Follow this Question
Related Questions
Raycast to align character on MeshCollider normals 1 Answer
Raycast missing moving Collider 1 Answer
Per-polygon ray tracing? 1 Answer
Raycast Collider Tag not returning correct result 1 Answer
Rays: why is not working? 1 Answer