How would I ray cast to the left and right side of a part

I am making a movement system, basically I am trying to detect the part in front, right and left of the character with ray cast. I manage to detect the part in front with lookvector but I have no idea how to do the sides (left, right) here is my script

local MainPart = script.Parent

while wait(0.5) do
	local FrontRay = Ray.new(MainPart.Position, MainPart.CFrame.LookVector * 50)
	local Fronthit = workspace:FindPartOnRayWithIgnoreList(FrontRay, {MainPart})
	
	if Fronthit then
		local distance = (MainPart.Position - Fronthit.Position).Magnitude
		local p = Instance.new("Part")
		p.Anchored = true
		p.CanCollide = false
		p.Material = "Neon"
		p.Size = Vector3.new(0.1, 0.1, distance)
		p.CFrame = CFrame.lookAt(MainPart.Position, Fronthit.Position)*CFrame.new(0, 0, -distance/2)
		p.Parent = game.Workspace
		print(Fronthit.Name)
		wait(0.2)
		p:Destroy()
	else
		print("no")
	end
end

Left and right side directions for rays can be achieved with rightvector. And you should be using workspace:Raycast() as it has more flexibility

local FrontRay = workspace:Raycast(MainPart.Position, MainPart.CFrame.LookVector * 50)
local RightRay = workspace:Raycast(MainPart.Position, MainPart.CFrame.RightVector * 50) 
local LeftRay = workspace:Raycast(MainPart.Position, MainPart.CFrame.RightVector * -50) --multiplied rightvector by -50 to become leftvector
4 Likes