Can't figure a way to offset lookvector properly for raycasting

Making bots for my game right now. I’m trying to give them a sort of vision based entirely on raycasting, and trying to offset the lookvector so they have a large field of view. I am failing miserably.

Yes, it works but only on specific orientations, such as 0,0,0 and 0,180,0. Other angles make the entire thing shoot rays at random places or a fixed position, seen below:

I’ve tried adjusting the ray distance, the orientations and all. No effect. I’ve tried looking around the forum for something similar to my problem, couldn’t find anything.

The code responsible for raycasting:

-- Visualizer and raycaster
function visualray(origin,result)
    --ray visualizer by @incapaz
	local point = Instance.new("Part",workspace)
	point.Size = Vector3.new(.5,.5,.5)
	point.Material = Enum.Material.Neon
	point.Transparency = .5
	point.Position = result
	point.Shape = Enum.PartType.Ball
	point.Anchored = true
	point.CanCollide = false
	point.CanTouch = false
	point.Color = Color3.new(0,1,0)
	local distance = (origin - result).Magnitude
	local ray = Instance.new("Part",workspace)
	ray.Material = Enum.Material.Neon
	ray.Color = Color3.new(1,0,0)
	ray.Transparency = .5
	ray.Anchored = true
	ray.CanCollide = false
	ray.CanTouch = false
	ray.Size = Vector3.new(0.1, 0.1, distance)
	ray.CFrame = CFrame.lookAt(origin, result)*CFrame.new(0, 0, -distance/2)
	game.Debris:AddItem(ray,task.wait())
	game.Debris:AddItem(point,.5)
end

function raycast(direction)
	local ray = workspace:Raycast(hum.RootPart.Position,direction,Rayparams)
	if ray then
		visualray(hum.RootPart.Position,ray.Position)
		if ray.Instance then
			if ray.Instance.Name == "BOT_BACKOFF" then
				return "laser",(ray.Position - char.Head.Position).Magnitude
			else		
				local possiblechar = ray.Instance:FindFirstAncestorOfClass("Model")
				if possiblechar then
					if possiblechar ~= workspace then
						if possiblechar:FindFirstChild("Humanoid") then
							local possibleplr = game.Players:GetPlayerFromCharacter(possiblechar)
							if possibleplr then
								if possibleplr.Team == game.Teams.Humans then
									return possiblechar, (hum.RootPart.Position - possiblechar.HumanoidRootPart.Position).Magnitude
								end
							end
						end
					end
				end
			end
		end
	end
end

--Sight checker, responsible for the rays seen in the video
function sight()
	for i = -90,90,5 do
		local pos = (char.Torso.CFrame.LookVector * 50) + Vector3.new(i,0,0)
		local result,dist = raycast(pos)
		if result then
			if result == "laser" and dist <= 10 then
				return "laser"
			else
				if dist <= 15 then
					return result, true
				else
					return result, false	
				end
			end
		end
	end
end

--char is the npc, hum is it's humanoid. bot_backoff is a trigger used for preventing bots from killing themselves.

If there’s an alternative, or better way to give the bots a vision, I’d love to hear it. Thanks in advance.

1 Like

Try change the code line

local pos = (char.Torso.CFrame.LookVector * 50) + Vector3.new(i,0,0)

to

local pos = (script.Parent.CFrame * CFrame.Angles(0,math.rad(i),0)).LookVector * 50

This will rotate your lookvector, i dont know if it is what you want

1 Like
local pos = char.LowerTorso.CFrame:PointToWorldSpace(50 * Vector3.new(math.sin(math.rad(i)),0,-math.cos(math.rad(i))))

I think I found some issues with your code. When the bot rotates, the raycasts don’t appear to rotate with the bot properly. I fixed it by changing this line local pos = (char.Torso.CFrame.LookVector * 50) + Vector3.new(i,0,0) to local pos = (Vector3.new(0, 0, -1) * 50) + Vector3.new(i,0,0).

Also, if you want racasts that cast rays in a circular shape, you can just set the pos variable to local pos = (CFrame.Angles(0, math.rad(i), 0):PointToWorldSpace(Vector3.new(0, 0, -1)) * 50).

I also have a place file that uses the suggestions that I gave you, and visualizes it:

Raycast Math Visualizer.rbxl (36.3 KB)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.