I raytrace in a direction, and it hits a wall. the raytrace keeps going, and at some point exits the wall again.
How can i find the length of the part of the raytrace which is inside the wall?
I raytrace in a direction, and it hits a wall. the raytrace keeps going, and at some point exits the wall again.
How can i find the length of the part of the raytrace which is inside the wall?
Wouldn’t that just be the thickness of the wall that you can grab from the mesh/collider?
Short of storing the thickness of the wall, you can’t without an opposing raycast, the data isn’t available.
Not sure if feasible in this situation, but you could raycast against layermask, and set up mesh colliders for in and out wall. At hitpoint of the in-wall, create new raycast in same direction. This ray will hit wall 2. The difference of hitpoint 1 and 2 is the thickness of that trajectory.
Quicker and easier would be to simply store approximate thickness in variable.
edit:
Silly me, if using 2 rays it’s clearly better to just create a raycast from other side to get the 2nd hitpoint as hippocoder suggested…
Yes, assuming i only want the exact thickness based on a perpendicular raycast.
However if, for instance, i hit the wall at an angle, it’s going to go through more than just the thickness of the wall, but also along some of it’s length., angles are important in this case.
Especially, too, for objects which aren’t completely regular walls. Like casting through a sphere (and not hitting the centre)
Is there any way to raycast to hit only a specific object? eg; that wall?
Layer mask.
Or, cheap and nasty - collect the hits (RaycastAll) and then iterate through them looking for something specific (i.e. that wall).
you can use collider.RayCast() wich will only cast against this collider. Somethink along these lines
if( Phyics.Raycast( ..., out firstHit) )
{
if( firstHit.collider.Raycast( oppoiteDirectionOfFirstRaycast, out secondHit )
{
var colliderThickness = Vector3.Distance( firstHit.point, secondHit.point );
}
}
Yeah, I’d go with what element_wsc says.
That second raycast on the collider, with the opposite direction ray, is going to need a origin far away from the hit point.
Issue, if it hit at a very acute angle, the distance through the solid could be very long, especially if it’s a very long wall or something. Imagine a cube scaled out several thousand units (for whatever reason) and you hit at 1 degree off the surface, that ray could easily travel the whole length of the cube.
Best option is to check the ‘bounds’ of the collider. Then select a point on the first ray that is outside the opposite side of the collider’s bounds, and cast backward from there.
Figured it out.