Hello!
I am currently trying to make an interaction system similar to Welcome to Bloxburg’s.
The player has to be within 20 studs and somewhat facing the item they want to interact with. The problem is that raycasting from the camera’s lookvector is extremely unreliable and seems not to work at all unless shift lock is being used. And that is unreliable as well.
Basically, if the player is looking at the interactable object with the camera and the camera only, the interaction prompt should show up.
Video raycasting from the camera:
Video raycasting from the HumanoidRootPart:
Here’s my script currently:
local raycastParameters = RaycastParams.new()
raycastParameters.CollisionGroup = 'RayCollisionGroup'
runService.RenderStepped:Connect(function()
local closestDistance = math.huge
local closestObject = nil
for i,interactionObject in pairs(interactableObjects) do
if interactionObject:IsA('Model') then
interactionObject = interactionObject.PrimaryPart
end
local ray = workspace:Raycast(currentCamera.CFrame.Position, currentCamera.CFrame.LookVector * minimumInteractionDistance, raycastParameters)
if ray then
if interactionObject == ray.Instance or interactionObject.Parent == ray.Instance.Parent then
if (humanoidRootPart.Position - interactionObject.Position).Magnitude < minimumInteractionDistance then
if (humanoidRootPart.Position - interactionObject.Position).Magnitude < closestDistance then
closestDistance = (humanoidRootPart.Position - interactionObject.Position).Magnitude
closestObject = interactionObject
end
end
end
end
end
if not closestObject then
template.Parent = nil
template.Adornee = nil
if focusedInteraction then
focusedInteraction:Disconnect()
end
if currentConnection then
currentConnection:Disconnect()
end
elseif closestObject ~= openInteraction then
promptInteraction(closestObject)
end
end)
Variables:
currentCamera = workspace.CurrentCamera
humanoidRootPart = game.Players.LocalPlayer.HumanoidRootPart.HumanoidRootPart