Hello,
I have been somewhat attempting the implementation of the following mathematical function without any luck.
function reflect(n,v) local n1 = n.unit local v1 = v.unit return 2*(n1:Dot(-1*v1))*n1 + v1 -- From the 'Developer’s Journal' blog tutorial post. end
Snippet of my code:
-- Tool is defined as well as the remote function. local ray = Ray.new(tool.Effector.CFrame.p, (mouse.Hit.p - tool.Effector.CFrame.p).unit * 120) local hit, position = game.Workspace:FindPartOnRay(ray, character) func:InvokeServer(hit, tool.Effector.CFrame.p,position, charge.Value) -- Basically draws the ray through a remote function. I have FilteringEnabled set to true.
How would I use this when raycasting and what do the arguments 'n' and 'v' need?
Well this is the simplest part of the code you need to get this working.
First of all the arguments are:
n - Normal vector(direction of the hit face) v - Direction of the ray(if I'm not mistaking)
Hardest thing to get, is the normal vector. There are scripts made by community, that does this very well. Here is first one I could find: http://pastebin.com/sXZ9FTNz
Note that this is not the fastest algorithm, but it will work good for your case.
To use it with ray casting, you would need to cast at least 2 rays(unless you want to have it reflect against several surfaces). Basically you find the part on ray, use the normal function, then the reflect function and finally cast a new ray , in direction that got returned by reflect function.