Hello I'm trying to cast three rays in total each at a different angle. The first ray (ray 2 in the picture) is easy, and the script I used to make that ray is this
local ray = Ray.new(char.HumanoidRootPart.CFrame.p, (char.HumanoidRootPart.CFrame.lookVector*5))
However, I'm trying to do that, but at an angle. To explain more thoroughly, I'll draw a picture here.
I am able to draw the Ray 2, but I tried using CFrame.Angles() to draw the Ray 1 and Ray 3 but couldn't figure it out. Any help is appreciated.
You can rotate the vector on the x and z axes like so using a little bit of trigonometry (or you can just plug your values into the formula in the webpage linked):
function rotatedVector (vector , theta) local x = (vector.X * math.cos(theta)) - (vector.Z * math.sin(theta)) local z = (vector.X * math.sin(theta)) + (vector.Z * math.cos(theta)) return Vector.new(x , Vector.Y , z) end
From here, it's relatively simple. Simply rotate the vectors by a certain angular offset from the lookVector
.
local theta = ... --angle in radians local lookVector1 = char.HumanoidRootPart.CFrame.lookVector * 5 local lookVector2 = rotatedVector(lookVector1 , theta) local lookVector3 = rotatedVector(lookVector1 , -theta) --Use these vectors to perform you ray casts local origin = char.HumanoidRootPart.CFrame.Position local ray1 = Ray.new(origin , lookVector1) local ray2 = Ray.new(origin , lookVector2) local ray3 = Ray.new(origin , lookVector3)
You can read up on rays more here and you can use these raycasts with the FindPartOnRay()
method of workspace.