Finding wall depth using Raycasting

I’m currently trying to make bullet penetration using raycasting but I’m stuck on this problem for while now.

I’m trying to find the length between the two intersection

image

The redline is just highlighting where the raycast is inside the part (the length between the intersection)

1 Like

Are the two blue parts supposed to be representing separate raycasts? Clarification is lacking.

Sorry for the lack of information I added a bit more information clarifying the situation.

So, this raycast is in a CollisionGroup that does not collide with the walls?

It’s usually done by moving forward a bit and ray casting backwards to find the exit point.

Use a Whitelist type with only the part that you hit in the whitelist.

Edit: I answered this before actually

1 Like

Here is a script I put together that will help with understanding how to do this.

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAppearanceLoaded:Wait()
local Mouse = Player:GetMouse()
local SubPart = game:GetService("ReplicatedStorage").SS

local function CreateParams(List, Type)
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = List
	params.FilterType = Type
	
	return params
end

local function CalculateDisatance(p1, p2)
	local dist = (p1 - p2).Magnitude
	return dist
end

local function CastAndFind(HitPos, Orign, Direction, Obj)
	print(Obj)
	local Cd
	local FirstHit = HitPos
	local SecondHit
	
	if Direction == nil then
		Cd = CFrame.new(Orign, HitPos).LookVector
	else
		Direction = Cd
	end
	
	local p = CreateParams({Obj}, Enum.RaycastFilterType.Whitelist)
	local WayLong = CFrame.new(Orign,HitPos) * CFrame.new(0,0,-100)
	local result = workspace:Raycast(WayLong.Position, (WayLong * CFrame.new(0,0,10000)).Position, p)
	print(result)
	
	if result ~= nil then
		print("Pass")
		SecondHit = result.Position
	end
	
	local Distance = CalculateDisatance(FirstHit, SecondHit)
	local newCF = CFrame.new((FirstHit + SecondHit) / 2, SecondHit)
	local NewP2 = SubPart:Clone(); NewP2.Parent = workspace; NewP2.CFrame = newCF; NewP2.Size = Vector3.new(.2,.2,Distance)
	
	print("P1")
	print(FirstHit)
	print("------------------------")
	print("P2")
	print(SecondHit)
	print("------------------------")
	print("Distance")
	print(Distance)
end


Mouse.Button1Down:Connect(function()
	CastAndFind(Mouse.Hit.Position, Character:WaitForChild("HumanoidRootPart").Position, nil, Mouse.Target)
end)

We first start by getting all the variables needed, mouse hit, mouse hit position, the origin. The main part is how to find the two points. Using Mouse.Hit.Position will work for finding the first position. Finding the second will be more difficult though. By using some CFrame we can get the CFrame look of the Origin and make it so it is far away, then I used RayCast to find where the point that intersects between the first hit and the second hit. Any questions let me know.

10 Likes

Is this the size of the part since you can maybe use that to find the thickness.