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
1
Question by Lolgab123 · Jan 19, 2015 at 07:56 AM · collidertriggerontriggerenterontriggerexitontriggerstay

How to use OnTriggerEnter with multiple triggered objects?

Hey guys! Here's the situation: I have a game in which you place blocks. You have a prespawn block that takes your mouse position. If this block is over another one already placed on the world, you will NOT be able to place it. So no blocks will be over others.

EVERYTHING Is in 2D space! But the prespawn block is over all of the other placed blocks!

Here's what I did: I put a script on the prespawn block that verify if there's a block trigerring it. I put:

 public bool onTrigger;
 
 void OnTriggerEnter2D()
 {
         onTrigger = true;
 }
 
 void OnTriggerExit2D()
 {
         onTrigger = false;
 }
 
 void OnTriggerStay2D()
 {
         onTrigger = true;
 }
 
 /*on other script*/
 void Update()
 {
         if(Input.GetMouseButton(0) && !onTrigger) Spawn();
 }

Here's the problem: The function OnTriggerExit2D is too fast, so even if the onTrigger is false for one single frame and then true during the other, it placed the block.

2nd problem: If I put 2 blocks one next to the other and I move my prespawn object from the exact position of the 1st block to the other, It will exit the first block's collider to enter the second's one. It's during that frame that onTrigger becomes false when it's not suppose to. So here's the 2nd question: is there a way I can use the OnTriggerExit2D function to set the variable onTrigger ONLY when there's absolutly NO block triggering? It will be really usefull!

Thanks

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
3
Best Answer

Answer by AndresBarrera · Jan 19, 2015 at 08:47 AM

Try these two things:

  1. Use FixedUpdate instead of Update, because OnTrigger events are updated at the same rate that physics events.

  2. Keep a count of triggers, and only Spawn when the count is 0

    public bool onTrigger; public int triggerCount = 0;

    void OnTriggerEnter2D() { onTrigger = true; triggerCount++; }

    void OnTriggerExit2D() { onTrigger = false; triggerCount--; }

Comment
Add comment · Show 4 · 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 Lolgab123 · Jan 19, 2015 at 11:54 AM 0
Share

Will definitly try this! Thanks! And what's the rate of the physics update? And does that means Fixed Update is faster or slower than Update?

avatar image AndresBarrera · Jan 19, 2015 at 12:05 PM 0
Share

It depends on different settings and the device running the game. FixedUpdate should execute at a constant rate (I think you can specify it on the physics settings for your project), while Update can execute at different rates, depending on how many frames per second are being processed

avatar image Lolgab123 · Jan 19, 2015 at 12:07 PM 0
Share

Oh ok, that's nice! So by using it, I'm sure it won't get to fast ;) Seriously, big thanks!

avatar image Lolgab123 · Jan 19, 2015 at 05:17 PM 0
Share

It definitly works! Thanks!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OnTrigger event when colliders are already touching eachother 1 Answer

Can someone clarifies OnTriggerEnter, OnTriggerExit and OnTriggerStay for me? (I have image included already) 0 Answers

Compound Triggers with no intermediary exit calls 1 Answer

How can I get my script to recognise when a collider exits while the box collider of the trigger object is disabled? 1 Answer

Trigger animation going crazy 3 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