Setups up interactables in a video game based on player distance from the object. Useful for showing "interact" buttons on anything from doors to cars etc.
Last active
September 28, 2021 05:43
-
-
Save elevenpassin/5c3559c4be573431608e0ffb2e65a816 to your computer and use it in GitHub Desktop.
Player sensitive interactables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local Interactables = game.Workspace.Interactables:GetChildren() | |
local CharacterRoot = script.Parent.HumanoidRootPart | |
local RunService = game:GetService("RunService") | |
local current = nil | |
for _, interactable in pairs(Interactables) do | |
local routine = coroutine.create(function(part1, part2, distance) | |
RunService.Stepped:Connect(function() | |
local d = (part1.Position - part2.Position).Magnitude | |
if d < distance then | |
if current then | |
current.BillboardGui.Enabled = false | |
end | |
current = interactable | |
else | |
interactable.BillboardGui.Enabled = false | |
end | |
if current then | |
current.BillboardGui.Enabled = true | |
end | |
end) | |
end) | |
coroutine.resume(routine, interactable, CharacterRoot, 10) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment