✓ Accepted answer
This would be a good use of the Dot Product
The dot product, also commonly known as the scalar product, is an operation preformed on vectors and returns a single number.
Example pulled from article:
local tool = script.Parent;
local handle = tool:WaitForChild("Handle");
function reflect(input, normal)
return -2 * input:Dot(normal) * normal + input;
end;
function drawray(ray, parent)
local part = Instance.new("Part", parent or game.Workspace.CurrentCamera);
part.FormFactor = Enum.FormFactor.Custom;
part.Material = Enum.Material.Neon;
part.Size = Vector3.new(.2, ray.Direction.magnitude, .2);
part.CFrame = CFrame.new(ray.Origin + ray.Direction/2, ray.Origin + ray.Direction) * CFrame.Angles(math.pi/2,0,0);
part.Anchored = true;
part.CanCollide = false;
part.BrickColor = BrickColor.new("Bright red");
return part;
end;
function fire(from, to, bounce)
local bounce = bounce or 0;
if bounce <= 5 then
local ray = Ray.new(from, (to - from).unit * 500);
local hit, pos, normal = game.Workspace:FindPartOnRay(ray, tool.Parent);
local ray2 = Ray.new(from, (pos - from));
local part = drawray(ray2, tool);
game:GetService("Debris"):AddItem(part, 2);
local ref = reflect((pos - from), normal);
if hit then
local hum = hit.Parent:FindFirstChild("Humanoid");
if hum then
hum:TakeDamage(math.random(10, 15));
end;
fire(pos, pos + ref, bounce + 1);
end;
end
end;
script.Parent.Equipped:connect(function(mouse)
mouse.TargetFilter = game.Workspace;
script.Parent.Activated:connect(function()
fire(handle.CFrame.p, mouse.Hit.p);
end);
end);