- Home /
Penetration weapon needs to find all objects it passes through
I am making a weapon where the bullet can sometimes pass through certain walls and hit items on the other side, and so far I have this code:
#pragma strict
private var me : Transform;
private var rigid : Rigidbody;
private var oldPos : Vector3;
function Start () {
me=transform;
rigid=rigidbody;
oldPos=me.position;
}
function Update() {
var entry : RaycastHit;
if(Physics.Linecast(oldPos,me.position,entry)){
var exit : RaycastHit;
if(Physics.Linecast(me.position,oldPos,exit)){
var thickness=CollisionThickness(entry,exit);
print(thickness);
}
}
oldPos=me.position;
}
function CollisionThickness (entry : RaycastHit, exit : RaycastHit) {
var colthickness=Vector3.Distance(entry.point,exit.point);
return colthickness;
}
which runs every frame on my colliderless rigidbody bullet to check whether it passed through an object that frame. It also finds how thick the object was it passed through, which I will use to calculate whether or no it should penetrate or not. But it often passes through multiple colliders in one go, and only detects the first and last entry/ exit points. This means it gives misleading thickness data, and will fail to place bulletholes on some surfaces when I have added that code in. What I want is for it to detect all entry/ exit points it goes through each frame, and get data from all of them. I tried Physics.RaycastAll but couldn't get it to work properly. Please could someone help with this as I have not been able to find a solution anywhere else?
Answer by Brian@Artific · Jul 15, 2013 at 03:35 PM
I think you were on the right track using Physics.RaycastAll - the trouble likely came from the fact that RaycastAll does not necessarily return a sorted array of hits. You can sort the hits by adding your own Comparison function:
static int CompareRaycastDistances(RaycastHit x, RaycastHit y)
{
if (x.distance > y.distance)
return 1;
if (x.distance < y.distance)
return -1;
return 0;
}
... then pass this to your sort method:
RaycastHit[] hits = Physics.RaycastAll(me.position, me.forward);
Array.Sort(hits, CompareRaycastDistances);
... so that you have a list of hits in order from the first collider to the last. For the exits, just run the same cast in the other direction and sort.
thank you, I did not even realise there was a RaycastHit.distance, and have been finding it the long way with Vector3.Distance up to now. That will work for raycast weapons with no travel time/ bullet drop, but I don't think it is possible to limit the range of a RaycastAll, so when I need to check all hits between point A and point B in each segment of a ballistic trajectory, it will ins$$anonymous$$d find all hits from point A through point B for infinite distance.
Your answer
Follow this Question
Related Questions
Simulating Bullet Gravity using Raycasting 1 Answer
EteeskiTutorials' bullets (raycast bullets with gravity) 1 Answer
Ricochet Problem of projectile bullets 0 Answers
Multiple raycasts in same fixedupdate 3 Answers
Simple Raycast Penetration 1 Answer