I was making a script to deflect blaster shots for my game when they hit a sword however the parts don’t face the direction that they were moving in.
Here is the script part that is important:
local Laser = Instance.new("Part", script.Parent)
Laser.CanCollide = false
Laser.Size = Vector3.new(1,1,1)
Laser.Color = Color3.fromRGB(255, 166, 76)
Laser.Material = Enum.Material.Neon
local BV = Instance.new("BodyVelocity", Laser)
Laser.Position = script.Parent.Position
Laser.CFrame = CFrame.new(Laser.Position, ClosestTarget.Character.HumanoidRootPart.Position)
script.Parent.CFrame = CFrame.new(script.Parent.Position, ClosestTarget.Character.HumanoidRootPart.Position)
local Filter = RaycastParams.new()
Filter.FilterDescendantsInstances = {script.Parent}
Filter.FilterType = Enum.RaycastFilterType.Blacklist
local Raycast = workspace:Raycast(script.Parent.Position, script.Parent.CFrame.LookVector * 50, Filter)
BV.Velocity = script.Parent.CFrame.LookVector * 10
local BoolValue = Instance.new("BoolValue", Laser)
local normal = script.Parent.CFrame.LookVector
game.Debris:AddItem(Laser, 5)
local Connection = Laser.Touched:Connect(function(Hit)
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
if table.find(Attacked, Player) == nil then
table.insert(Attacked,1,Player)
Player.Character.Humanoid:TakeDamage(20)
spawn(function()
wait(1.5)
table.remove(Attacked, table.find(Attacked, Player))
end)
Laser:Destroy()
end
elseif Hit.Name == "Blade" and Hit.Parent:IsA("Tool") then
if Raycast and Hit.Name ~= "Droid" and Hit ~= Laser and BoolValue.Value == false then
Laser.Anchored = true
BoolValue.Value = true
local reflectedNormal = normal - (2 * normal:Dot(Raycast.Normal) * Raycast.Normal)
normal = reflectedNormal
Laser.CFrame = CFrame.new(Laser.Position, reflectedNormal) -- THIS part is not making the part face where it is headed.
Laser.BodyVelocity.Velocity = reflectedNormal * -10
Laser.Anchored = false
end
end
end)