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
end
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
animateBillboardIn(replicatedBillboard)
table.insert(pendingRemoval,replicatedBillboard)
currentAwaitingObject,currentAwaitingTag,currentAwaitingBillboard = closestInteractionObject,connectedTag,replicatedBillboard
end
else
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
end
end