I’m trying to create a gun system, while using a revamped version of Ray Reflection to achieve some extra functions for special guns, but currently, i have a problem. Sometimes the Raycast completely ignores some parts, the bullet doesn’t reflect, and continues moving forward, not sure the reason of why, the bullet movement function works with Stepped, and it’s supposed to constantly Raycast to check if there’s a wall or something there, here is the main function of the script (If it isn’t sufficient information, then i can give you the full script):
local Bullet = game.Workspace.Bullet
local StudsPerSecond = 10
local CurrentPosition = script.Parent.Position
local CurrentNormal = script.Parent.CFrame.LookVector
game:GetService("RunService").Stepped:Connect(function(_A, StepTime)
local Speed = StepTime * StudsPerSecond
local Position
local Direction = CurrentNormal * (OverrideDistance or Speed)
--//Create Ray:
local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
RayParams.FilterDescendantsInstances = {script.Parent, game.Workspace:FindFirstChild("Bullets")} --// This is supposed to ignore the current bullets in game and the current gun part that fires the bullet.
local Result = game.Workspace:Raycast(CurrentPosition, Direction)
--//Formula:
if Result then
Position = Result.Position
else
Position = CurrentPosition + (CurrentNormal * Speed)
end
local OldPos = CurrentPosition
CurrentPosition = Position
if Result then
local ResultNormal = Result.Normal
local Reflect = (CurrentNormal - (2 * CurrentNormal:Dot(ResultNormal) * ResultNormal))
CurrentNormal = Reflect
OverrideDistance = (StudsPerSecond / 10 + 1) - (Position - OldPos).Magnitude
end
--// Move bullet:
Bullet.CFrame = CFrame.new(CurrentPosition, Bullet.Position)
end)
I tried to make stuff such as revamping the script, making the ray longer, or change the formula a bit, however, nothing helped, or it wasn’t efficient. I’m not sure what can i do, any help is really appreciated. Thanks for reading.