User Tools

Site Tools


guides:proximity-trigger

Custom Component: Proximity Trigger

local COMP_PROXIMITY_TRIGGER = {
	TypeName = "COMP_PROXIMITY_TRIGGER",
	ParentType = "COMPONENT",
	Properties = {}
}
 
function COMP_PROXIMITY_TRIGGER:update()
    -- Position of the Prefab the component is assigned to
    local pos1 = self:getOwner():getGlobalPosition()
 
    -- Using COMP_AGENT to include non-villagers too like the Envoy
    self:getLevel():getComponentManager("COMP_AGENT"):getAllComponent():forEach(
        function(comp)
            -- Position of the Agent
            local pos2 = comp:getOwner():getGlobalPosition()
 
            -- Computing distance between Agent and Prefab
            local distance = math.sqrt( (pos1.x - pos2.x)^2 + (pos1.y - pos2.y)^2 + (pos1.z - pos2.z)^2 )
 
            if distance < 4 then -- you can set this distance as close to the object as you need
                -- Do fancy stuff
            end
        end
    )
end
guides/proximity-trigger.txt · Last modified: 2021/02/23 12:05 by minotorious