Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by tanoshimi · Dec 11, 2016 at 12:18 PM · ontriggerenterinventory

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Dec 11, 2016 at 05:58 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

60 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges