- Home /
Trigger A should trigger before trigger B, but B triggers first
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.
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.
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.
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.
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.
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 :-)