Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Turkeybag · Sep 10, 2013 at 06:42 AM · triggerdetect

A way for a trigger to detect if anything is in it?

Hard to describe what I mean in the title but can you get a trigger to do something if anything is in it. Say that the player is on a space ship with artificial gravity, and if anything enters a trigger in say the power-core then the player gets an upward force to counter gravity. Pretty much you throw something in the core, artificial gravity stops working.

Closest thing I could find is OnTriggerStay but if 20 different objects were in the core wouldn't it be called 20 times throwing the player upward? I know I could get it to set a property to stop gravity but what if there were 1000 or more objects in the core. Seems pretty costly. Is there a simpler way that will be called every frame so long as something remains in the trigger?

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
0

Answer by tanoshimi · Sep 10, 2013 at 06:55 AM

Create a Boolean variable called "IsSomethingInTheCore", initialised as false. Do your IsTriggerStay() checks and, if there are any objects within the trigger, set the variable to true. Then, at the end of the update loop, jump the player based on the Boolean value - it won't matter how many objects there were - just that at least one caused it to be set (which I think is what you're describing)

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 Turkeybag · Sep 10, 2013 at 08:05 AM 0
Share

That kind of works but it makes it needlessly difficult when it comes to turning it back off. I guess I could do it at the end of the update function but I'm not sure if unity would do the OnTriggerStay event before or after. That's not my biggest problem anyway.

Say I was using the trigger to detect if there is anything important in an area of a free-roam game (player, AI, bullet, placeable player items etc). Using OnTriggerStay would work until the player goes to the area and and then there are AIs, bullets, player places objects, and all sorts of things everywhere. The trigger would be changing the property for the area maybe 500 times every frame to the same thing. It just seems like there has to be a better way. OnTriggerStay calls what it does every frame for every object in it. I just want a the trigger to call something once every frame so long as something is in it. If there is one thing there it gets called once, and if there are 500 things there it still gets called once.

Even something that I can use in a if statement like SomethingInTrigger would work wonders.

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

16 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

Related Questions

How to dectect Cube (Trigger) is touched 1 Answer

Volume detection. 0 Answers

Detect with Object has been triggered 3 Answers

Can't click gameobject when over another trigger? 1 Answer

Collide with projection 1 Answer


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