Reflecting a ray

Hello, I am trying to make a mirror and when you shoot it with a laser, the laser beam reflects. I have the shooting part done, but can’t seem to figure out how to reflect it.

I’ve tried some solutions on devforum posts but it doesn’t work and I cannot figure out the math.

LocalScript code.

local UIS = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

UIS.InputBegan:Connect(function(input, gPe)
	if gPe then return end
	if input.KeyCode == Enum.KeyCode.Q then	
		local ray = game.Players.LocalPlayer:GetMouse().UnitRay
		local _, position, normal = workspace:FindPartOnRay(ray, player.Character)
 		local hit = mouse.Target

		print(hit)
		
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("White")
		beam.Material = "Neon"
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
		
		script.Laser:Play()
 
		local distance = (mouse.Hit.p - position).magnitude
		beam.Size = Vector3.new(0.1, 0.1, distance)
		beam.CFrame = CFrame.new(mouse.Hit.p, position) * CFrame.new(0, 0, -distance / 2)
		
		if hit then
			if hit.Name == "Mirror" then
				print("Hit mirror") -- no idea what to do after this point.
			end
		end
		
		for i = 0,1,0.1 do
			beam.Transparency = i
			wait()
		end
		
	end
end)
2 Likes

I’d suggest to take a look into already existing tutorials regarding that.

2 Likes

Thanks, I already tried looking through that but didn’t really understand how it works. I’ll try to look through that topic again though.

1 Like

This might help as well.

https://github.com/EgoMoose/Articles/blob/master/Vectors/Dot product.md
It has something on reflecting rays at the end.

3 Likes

It says page not available :sweat_smile:

Yeah sorry, I fixed it. Make sure to have a space between “Dot” and “product.md”

1 Like

You didn’t fix it, though? Just use URL encoding:
https://github.com/EgoMoose/Articles/blob/master/Vectors/Dot%20product.md

3 Likes

ohh alright. I’ll look into that
30 characters

Funny enough I was trying to add that 20 percent but I have this weird thing with my phone where the percent symbol can’t be added xd

1 Like