Hey there - I’m attempting to make a laser gun that bounces off of walls.
I have this problem where if I fire in one direction, it’ll reflect perfectly, but if it’s the opposite direction then the ray will just keep going straight and if it’s anything in between the reflection will work but be incorrect depending on the angle.
Here’s my code for calculating the reflection:
local Raycast = workspace:Raycast(
origin,
((direction - origin).Unit) * maxdist,
params)
local Position
local Part
local Distance
local Normal
local Reflect
if not Raycast then
Distance = maxdist
Position = origin + (((origin - origin).Unit) * maxdist)
else
Position = Raycast.Position
Part = Raycast.Instance
Distance = (origin - Position).magnitude
Normal = Raycast.Normal
Reflect = direction - 2 * (direction:Dot(Normal)) * Normal
direction = Reflect
end
What am I doing wrong here?