How do I measure the part thickness using a ray cast?

How do I measure the part thickness using a ray cast?
I am trying to do a bullet piercing system using ray cast system but the bullet goes through the part regardless of its thickness, how do I measure the part thickness using a ray cast?
I have tried nothing so far because I don’t know how to do it.

2 Likes

That shouldn’t be happening, may we see your code? My guess is that you are raycasting incorrectly.

The ray cast check if the part material is the right material, if it is, the ray will ignore the part and pierce through it, but I wanted the ray to measure the part thickness too.
Here’s the code you wanted:

local function CanPierce(HitPart, HitPoint, Normal, Material)
	if Material == Enum.Material.Ice or Material == Enum.Material.Wood or
           Material ==  Enum.Material.Glass or Material == Enum.Material.WoodPlanks or
               HitPart.Transparency >= 0.9 or HitPart.Name == "Handle" then
					if HitPart.Transparency < 0.9 then
						  MakeImpactFX(HitPart, HitPoint, Normal, HitEffectData, BulletHoleData)
						end
		
			return true -- Can pierce
	end
	return false -- Can't pierce
end
1 Like

Is it just me or do you check if the transparency is less than 0.9 after verifying that it’s more than 0.9?

I wanted the impact effect to emit if the part transparency lower than 0.9 but if the part transparency more than 0.9 its will pierce through the part but does not emit the effect

I just realized that it says “or” not “and” so so it might be less than 0.9 my mistake

I guess it’s not possible…

I can think of a some ways but they are quite complicated.

  1. Simplest but with some error is to just check on what surface the brick was hit and then check the according height, width or length of the brick. This is would work alright for when the bullet enters from a face and leaves the opposite face of the brick (still a bit of error, but good enough). However, there is a problem for when the bullet enters one surface and exits via some other adjacent surface of the brick. The error will be very high in that scenario.

  2. There is a way that always works but requires some additional programming. You can shoot a ray until it hits a part. Save where that part was hit. Then shoot a ray in the opposite direction from far away and use whitelist to whitelist just the part that was hit. Now you have the 2 points where the bullet would enter and exit. You can calculate the magnitude and find out the thickness of a part.

Very interesting question!

EDIT: I added a picture to illustrate better.
Red Point: You find with normal raycast
GreenPoint: You find with inverse raycast

10 Likes

I am trying to do the 2nd way but I can’t get it to work.

Show some code to see where the problem is?

It was messy I removed it. I will try to rescript it.