I am trying to raycast rays at an angle from the characters head, in the direction they are looking, but simply cannot figure out a way to do it. I drew out a picture to visualize it better.
The blue arrow is the raycast, I am able to get, which I will leave the code for here:
local ray = Ray.new(character.Head.CFrame.p, character.Head.CFrame.lookVector * 6)
local part, position = workspace:FindPartOnRayWithIgnoreList(ray, characters)
This code works great, but when I try to apply an angle so I can get results like the red arrow, it doesn’t quite work.
local rayAngle = Ray.new(character.Head.CFrame.p, CFrame.Angles(math.rad(20), 0, 0).lookVector * character.Head.CFrame.lookVector * 6)
local anglePart, position = workspace:FindPartOnRayWithIgnoreList(rayAngle , characters)
Does anyone have any idea how to go about this? Thank you so much in advance.
You need to multiply the cframe of the player’s head by the chosen angle first then get the look vector, so something like this should work:
local Angle = 20
local Length = 999
local Rotation = CFrame.Angles(0, math.rad(Angle),0)
local rayAngle = Ray.new(character.Head.CFrame.Position, (character.Head.CFrame * Rotation).lookVector * Length)
local anglePart, position = workspace:FindPartOnRayWithIgnoreList(rayAngle , characters)
yes, you can visualize a ray by doing something like:
local VisualRay = game.Workspace.Part ---- or create a new part
local StartPos = rayAngle.Origin
local Endpos = StartPos + (rayAngle.Direction)
local distance = 10 ---- or the actual length which is (StartPos - Endpos).Magnitude
VisualRay.Size = Vector3.new(0.3, 0.3, distance)
VisualRay.CFrame = CFrame.new( StartPos , Endpos) * CFrame.new(0, 0, -distance *.5)
it seems to be working for me when using the same thing i provided above, you were wanting something like this right?
(part size is shorter than the actual ray of course)