Hi, i wanted to make a proximity prompt visible Only when looking at a part. I have no idea how to do this tho. If you have any questions - ask me and i will respond. I will be happy for any response
1 Like
Hi! I’ve found this post which explains how to detect whether a part is in the screen or not (or if the player is looking at it).
You can then enable or disable the proximity prompt according
2 Likes
As @SmartestDev said, If you use that script, these are some scripts you can make with example scripts
- Enable Proximity If Player Cam is including the part
local part = workspace:FindFirstChild("Your Part Name") -- Get Your Part
local proximity = part:FindFirstChild("ProximityPrompt") -- Find Proximity Prompt in your Part
local cam = workspace.CurrentCamera -- Get Player Camera
if part and proximity then -- If part and proximity exist
proximity.Enabled = false -- Unenable Proximity Prompt When Proximity Loaded
while true do -- Repeat Forever(loop)
task.wait(0.5) -- Wait 0.5 seconds to prevent from lagging
local isOnScreen = select(2, cam:WorldToViewportPoint(part.Position)) -- Checking if Player Cam is looking at the Part you selected(line 1)
if isOnScreen == true then -- If Yes then
proximity.Enabled = true -- Enable Proximity Prompt
else -- Else
proximity.Enabled = false -- Unenable Proximity Prompt
end
end
end
- Enable Proximity If player is looking at the part(using look vector)
local part = workspace:FindFirstChild("Your Part Name") -- Get Your Part
local proximity = part:FindFirstChild("ProximityPrompt") -- Find Proximity Prompt in your Part
local cam = workspace.CurrentCamera -- Get Player Camera
local length = 100 -- Length For Camera
local cfg = RaycastParams.new() -- Get RaycastParams
local print_what_player_is_looking_at = true -- If true, It prints the part name what player is looking at
if part and proximity then -- If part and proximity exist
proximity.Enabled = false -- Unenable Proximity
while true do -- Repeat Forever(loop)
task.wait(0.5) -- Wait 0.5 seconds to prevent from lagging
local cast = workspace:Raycast(cam.CFrame.Position, cam.CFrame.LookVector * length, cfg) -- Get what Player is looking at
if cast and cast.Instance then -- If Player is looking at something and something is an object then
if print_what_player_is_looking_at == true then -- If player set setting to true(line 7)
print(cast.Instance) -- print obejct name
end
if cast.Instance == part then -- If player is looking at the part you selected then
proximity.Enabled = true -- Enable Proximity
else -- Else
proximity.Enabled = false -- Unenable Proximity
end
end
end
end
3 Likes
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.