- Home /
Suppress OnTriggerEnter on Instantiation
tldr: Can anyone suggest an elegant way to prevent OnTriggerEnter being fired if an object is instantiated inside a trigger collider? (i.e. I want the rigidbody to have to leave the trigger area and re-enter it before it is called)
Explanation: My player character automatically collects items as she walks over them. Using OnTriggerEnter, the objects are destroyed in the scene and added to an inventory. If the player then decides to drop an item from the inventory, the gameobject is re-instantiated in the scene again at the player's feet. Nothing particularly unusual. The problem is that this causes the item to be immediately recollected and placed back into the inventory again. My simple workaround at the moment is simply to place dropped items a short distance away from the player (i.e. outside their collider area), but this is unpleasant, since we'd like the player to show an animation of them actually placing the item down on the floor when discarding an item.
I'm aware that obviously I could use a sort of counter and only collect dropped items on the second time that OnTriggerEnter fires, but that seems very cludgy...
Any ideas gratefully received!
Answer by Bunny83 · Dec 11, 2016 at 03:01 PM
Well you can't suppress OnTriggerEnter. If it would be possible it would create an inconsistency with OnTriggerStay and OnTriggerExit.
There are several ways to solve this, however it depends on which side you actually do the pick-up logic. Is it on the player or on the object?
If it's on the object (or if it somehow can influence the pickup), the easiest way would be to implement it on the object itself.
If the pickup logic is entirely on the player the solution is basically the same but a bit more complicated as the player would have to keep track of all objects he puts down.
Solution1 (the minecraft solution): Time
Minecraft for example uses a timeout value of about a second i think. During that time the object can not be picked up. Again this would be best implemented on the object itself. Placing the pickup logic on the player inside OnTriggerStay and in addition "asking" the object you want to pick up if it can be picked up would probably be the best way to copy the minecraft behaviour.
Solution2: tracking reference / List
For this solution the object would have a reference to the player object which should be ignored until OnTriggerExit is called with that tracked object in which case the reference would be set to null.
A pure player side solution would use a List or Dictionary. So when the player creates / "drops" an object, it will add the object to the ignore list. Inside OnTriggerEnter you check that the triggered object is not inside the ignore list. When OnTriggerExit happens you simply remove that object from the list. So on re-entry it would be collected again.
Thanks for the suggestions, @bunny83. Yes - we did consider a cooldown period as one option, but it seems somewhat arbitrary and I'm not sure it would work for our game - the player is not always moving so you'd get the situation where they sorted out their inventory, dropped some items, started doing something else and then the character would decide to pick up the item again ;) Your example of $$anonymous$$inecraft I believe also does the "dropping items slightly out-of-reach" that we do too?
I think your list might be the best approach, and would be relatively easy to implement player-side - I was just hoping there would be something that wouldn't feel like such a workaround!
Your answer
Follow this Question
Related Questions
Object being destroyed without GetKey Down 1 Answer
OnTriggerEnter / Inventory question?! 1 Answer
Key and Door Tutorial Help? 2 Answers
Save an array to file 1 Answer
Confirming item pickup in inventory 0 Answers