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