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 hatake3 · Sep 12, 2014 at 04:03 PM · collidercollider2dontriggerenter2d

Trigger A should trigger before trigger B, but B triggers first

alt text alt text

Not sure if you can see them, but there are edge colliders at both sides of the purple rectangle. The purple rectangle has a box collider. The colliders are working fine EXCEPT for one thing. When the ball (barely visible red thing on rectangle) moves from paddle and onto rectangle, it sometimes (but not always) triggers the box collider on the rectangle BEFORE it triggers the edge colliders on corresponding side.

Note that the ball does not move fast (time for moving from paddle to paddle ~ 2-3 secs). I also think there is plenty of space between edge colliders and box collider -- obviously not so, it seems. Anyone has any idea why this is happening?

Here is the relevant code. The script on the edge colliders is very short (script for right trigger is identical):

 public class LeftLaneTrigger : MonoBehaviour {
 
 
     public BoostChecker checkerScript;
     
     void OnTriggerEnter2D(Collider2D other)
     {
         Debug.Log ("left trigger");
     
         if (other.gameObject.name == "Ball")
             checkerScript.TriggerLeft ();
     }
 }


Here is the script checking triggers on the box collider. Not really important, but you can see I do the Debug.Log:

 void OnTriggerEnter2D(Collider2D other)
     {
         if (ballScript.LastPlayerTouched == "player1")
             Debug.Log ("lane trigger");
     
         // If ball enters boost lane from the left, we check if player1 gets the boost
         if (leftTrigger && other.gameObject.name == "Ball" &&
             ballScript.LastPlayerTouched == "player1")
         {
             ballInLane = true;
         }
         
         
         // If ball enters boost lane from the right, we check if player2 gets the boost
         
         if (rightTrigger && other.gameObject.name == "Ball" &&
             ballScript.LastPlayerTouched == "player2")
         {
             ballInLane = true;
         }
     }


Finally, the Update method moving the ball:

 void Update ()
 {
     // Move ball
     Vector3 position = transform.position;
     position.x += ballXSpeed * Time.deltaTime * 40;
     position.y += ballYSpeed * Time.deltaTime * 40;
     transform.position = position;
     
     // Limit max x- and y-speed of ball
     if (ballXSpeed > 0.2f)
         ballXSpeed = 0.2f;
     else if (ballXSpeed < -0.2f)
         ballXSpeed = -0.2f;
     
     if (ballYSpeed > 0.3f)
         ballYSpeed = 0.3f;
     else if (ballYSpeed < -0.3f)
         ballYSpeed = -0.3f;
 }


Just to be clear: When the ball moves from the left paddle and onto the rectangle, the debug statements often print: "lane trigger" "left trigger"

In that order. "lane trigger" is the box collider, and "left trigger" is the edge collider to the left of the box collider.

debug_colliders.png (61.6 kB)
debug_hierarchy.png (8.0 kB)
Comment
Add comment · Show 5
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 smoggach · Sep 12, 2014 at 05:10 PM 1
Share

What's the hierarchy of the objects like? The best way I can think of to make it work is to have both edge colliders on one object then have the box collider as a child. This way the edge will always get the OnCollision2D first.

avatar image hatake3 · Sep 12, 2014 at 05:25 PM 0
Share

I added a picture of the hierarchy at the top of the question. As it is, it's the other way around; the edge collider objects are children of the object with the box collider ("SlowZoneBoostLane"). But this shouldn't make the box collider trigger first? There are still "correct" cases, where the edge colliders triggers first, but it seems to be random.

avatar image hatake3 · Sep 12, 2014 at 06:00 PM 0
Share

I just tested with placement of left edge collider. The left side of the box collider (on purple rect) has an x-position of -4.60 (world units).

When left edge collider is at -4.65, the box collider is consistently being triggered first.

When left edge collider is at -4.75, as it is in the picture above, it seems to be about 50/50 which collider is triggered first.

When left edge collider is at -4.85, it consistently triggers first.

Sadly, .25 units is a relatively large amount and I need it to be closer to the box collider.

avatar image smoggach · Sep 12, 2014 at 06:44 PM 1
Share

In that case then I'd recommend making it work with just one box collider. You might also try skinny box colliders ins$$anonymous$$d of edge colliders. Switching to box colliders usually solves the problem for me when other colliders aren't producing the desired behaviour.

avatar image hatake3 · Sep 12, 2014 at 07:05 PM 0
Share

Switched to box collider with width 0.1 world units. Didn't work at x-position -4.65, but worked consistently at -4.75. It's weird, though. $$anonymous$$ust be a logical reason... Thanks for the help :-)

0 Replies

· Add your reply
  • Sort: 

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

24 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

Related Questions

Unexpected token: collider? 1 Answer

OnTriggerEnter2D works without being actually triggered. 0 Answers

Trigger only one collider? 1 Answer

OnTriggerExit2D not working on android (but works in editor) 0 Answers

Physics2D.OverlapCircleAll returning colliders outside of the area 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