(FastCast) ray goes straight through a part even though it Ricocheted

making a Ricochet system but every time the bullet reflects off of a part it’ll ricochet but it also goes straight through the part. Fastcast would activate OnRayPierced two times which would proceed to do both of the if conditions Pierced or Ricochet.

red: is what i expect it to do/go
green: is what is doing


function OnRayPierced(cast, raycastResult, segmentVelocity, cosmeticBulletObject)
	local bulletData = cast.BulletData
	local canPenetrate = bulletData["CanPenetrateArmor"]
	local isWallBang = raycastResult.Instance:GetAttribute("WallBang") == true
	local armorSettings = raycastResult.Instance:GetAttribute("ArmorSettings")

	if not canPenetrate or isWallBang or armorSettings == "Air" then
		return
	end
	
	local function Reflect(surfaceNormal, bulletNormal)
		return bulletNormal - (2 * bulletNormal:Dot(surfaceNormal) * surfaceNormal)
	end

	local position = raycastResult.Position
	local normal = raycastResult.Normal
	local newNormal = Reflect(normal, segmentVelocity.Unit)
	local angleOfAttack = math.deg(math.acos(newNormal:Dot(normal)))

	print(angleOfAttack, cast.BulletData.RicochetAngle, raycastResult.Position)

	if angleOfAttack <= cast.BulletData.RicochetAngle then
		-- Pierced 
		if cast.BulletData.ArmorDamage then
			cast.BulletData.PenetrationDamage = cast.BulletData.ArmorDamage 
		end
		print("Shell Able To Pierce Through Armor With ", cast.BulletData.PenetrationDamage)
		local Part = CreatePart(Vector3.new(.1, .1, .1), .5, Color3.fromRGB(255, 0, 0), Enum.Material.Neon)
		Part.CFrame = CFrame.new(raycastResult.Position, raycastResult.Position + raycastResult.Normal)
		
		local PenetrationModule = require(game.ReplicatedStorage.MainModules["Server_Penetration_Module_4.0"])
		PenetrationModule:new(raycastResult, cast.BulletData)
		cast.UserData.Hits = 10
	else
		-- Ricochet
		print("Shell Ricocheted")
		local Part1 = CreatePart(Vector3.new(.5, .5, .5), .5, Color3.fromRGB(85, 85, 127), Enum.Material.Neon)
		Part1.CFrame = CFrame.new(raycastResult.Position, raycastResult.Position + raycastResult.Normal)

		local Part2 = CreatePart(Vector3.new(.1, .1, 10), .5, Color3.fromRGB(255, 170, 0), Enum.Material.Neon)
		Part2.CFrame = CFrame.new(raycastResult.Position, raycastResult.Position + newNormal) * CFrame.new(0,0,-5)
		
		local Part3 = CreatePart(Vector3.new(.3, .3, .3), .5, Color3.fromRGB(0, 170, 0), Enum.Material.Neon)
		Part3.Position = CFrame.new(raycastResult.Position, raycastResult.Position + newNormal).Position
		
		cast:SetVelocity(newNormal * segmentVelocity.Magnitude)
		cast:SetPosition(position)
	end
end
2 Likes

SORRY FOR BUMPING OLD TOPIC BUT…
Are you running the OnRayPierced function on the client? When running it on the client, it’ll usually cause weird things like this to happen. I’m still trying to figure out a solution.

2 Likes

sorry for the late reply!
i ended up creating my very own bullet system, fastcast is a quick and awesome module it just has its problems here and there. to answer your question I was running “onraypierced” on server-sided OOP

1 Like

Ahh, Fair enough. I also ended up making my own bullet system myself, as fastcast wasn’t supporting all of my needs.