Raycasting Inaccuracies when attempting to detect obstructions

Hello! I’m currently trying to code a keyboard interaction system for my game. It’s proximity-based within 10 studs of any interaction tagged object, you’ll get the prompt for the closest object. The issue I’m having is using trying to determine if there are any obstructions between the player and the object. An example would be a wall. The way I’m trying to do this is using raycasting, casting two rays, one from the torso of the player, and one from the camera. However, using this it seems like the rays are detecting things not even remotely close to the object. I’ve compiled a gif and each brick you see floating is where one of the rays returned an obstruction. Any help is appreciated, thanks!

Within the gif above, I’m trying to get the prompt for the lockers, however, it’s saying the walls around it are obstructing the prompt.

Code Snippet:

local localCamera = Workspace:FindFirstChild('Camera');
			local localCharacter = LocalPlayer.Character;
			local LocalRootHumanoid = localCharacter:WaitForChild('HumanoidRootPart');
			local objectVectorPosition
			if closestInteractionObject:IsA('Model') then objectVectorPosition = closestInteractionObject:GetModelCFrame().p else
				objectVectorPosition = closestInteractionObject.Position
			local obstructionRayCamera = Ray.new(localCamera.CFrame.p,objectVectorPosition)
			local obstructionRayTorso = Ray.new(LocalRootHumanoid.Position,objectVectorPosition)
			local obstructionIgnores = {localCharacter,closestInteractionObject}
			local isObstructedOnCamera,cameraObstructionVector = Workspace:FindPartOnRayWithIgnoreList(obstructionRayCamera,obstructionIgnores)
			local isObstructedOnTorso,torsoObstructionVector = Workspace:FindPartOnRayWithIgnoreList(obstructionRayTorso,obstructionIgnores)
			if not isObstructedOnCamera or not isObstructedOnTorso then
				if authenticatePopup(connectedTag) then
					local actionName = string.gsub(connectedTag.actionName,'{OBJNAME}',closestInteractionObject.Name)
					local replicatedBillboard = ConfigurationModule['INTERACTION_BILLBOARD']:Clone()
					replicatedBillboard:FindFirstChild('animationFrame'):FindFirstChild('actionName').Text = actionName
					replicatedBillboard.Parent = closestInteractionObject
					currentAwaitingObject,currentAwaitingTag,currentAwaitingBillboard = closestInteractionObject,connectedTag,replicatedBillboard
				local obstructionVisualization = Instance.new('Part',Workspace)
				obstructionVisualization.Anchored = true
				obstructionVisualization.Size = Vector3.new(0.5,0.5,0.5)
				obstructionVisualization.Position = cameraObstructionVector
				obstructionVisualization = Instance.new('Part',Workspace)
				obstructionVisualization.Anchored = true
				obstructionVisualization.Size = Vector3.new(0.5,0.5,0.5)
				obstructionVisualization.Position = torsoObstructionVector	

I’ve tried looking into this some more and still cannot find anything.

After you place an indicator block do you add it to the obstructionIgnores list?

Have you tried to checking it without not isObstructedOnCamera? And seeing if it works with the Torso to object?

I’d use region3 to limit to the objects close to me and then check if they are in my camera’s point of view with camera:WorldToViewportPoint

Not sure if this is the fix but i did find an issue with your code.

Raycasts dont take two positions, as seen here. They take the start position, then the direction of the ray. (Ray.new(startPosition,direction*rayDistance))

So, you gonna want to get the direction from the camera to the object by doing something like localCamera.CFrame.p-objectVectorPosition and plug that into the direction of the ray instead of just objectVectorPosition and that should fix it up good probably maybe hopefully idk.

1 Like

Hey! So I took your fix and tried it, and it didn’t work so I did some more research on directions and found that be doing equation like so, InteractionObjectPosition - StartPoint, It works flawlessly. Thanks so much for your help!

1 Like

ah yes, my bad lol. im always getting equations wrong somehow. Glad to hear that i helped :ok_hand:

1 Like