Triggers will fire for any object passing through them that have a TriggerActivating attribute. If we want to create Triggers that only react to certain objects, we'll need to write some code to filter objects out. For instance, let's say we have a special object called 'Player' that we want a Trigger to fire for but for nothing else. Here's how we could achieve it:
function Trigger:onEnter(obj) -- Reject objects not named Player if (obj:getName() ~= "Player") then return end print("We have a Player") end
Another method of filtering could be to check to see if the object is of a particular type.
function Trigger:onEnter(obj) -- Reject if our object is not a Camera if (not obj:isTypeOf("Camera")) then return end print("We have a Camera") end
Another check could be to reject objects that don't have RigidBodies:
function Trigger:onEnter(obj) -- Reject objects that don't have RigidBodies if (not obj:getRigidBody()) then return end print("We have an object with a RigidBody") end
Checking to see if an object has a particular type of attribute is important so we don't go accessing functions and properties that aren't there which will through an error.
Comments
0 comments
Please sign in to leave a comment.