User Tools

Site Tools


guides:proximity-trigger

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
guides:proximity-trigger [2021/02/23 11:47] – created minotoriousguides:proximity-trigger [2021/02/23 12:05] (current) – initial article minotorious
Line 1: Line 1:
-====== Custom Component Proximity Trigger ======+====== Custom ComponentProximity Trigger ====== 
 + 
 +<code lua> 
 +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 
 +</code>
guides/proximity-trigger.1614098867.txt.gz · Last modified: 2021/02/23 11:47 by minotorious

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki