• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OnTriggerExit is not calling 2 Answers

Audio not playing after OnTriggerStay is applied to the scripts (Driving Simulator Project) 0 Answers

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

OnTrigger event when colliders are already touching eachother 1 Answer

Compound Triggers with no intermediary exit calls 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges