- Home /
Update scaling and 2d collider/trigger not firing
I have a snowball object that has a 2d rigidbody with a circle collider on it.
When the snowball travels it grows using scale
//Used on LateUpdate() in SnowBall
float travelDist = Vector2.Distance(transform.position, m_lastPosition);
if (m_curBallSize < EndSize && travelDist > 0.01f)
{
m_grownSize += travelDistDelta / 10000f;
SetBallSize(m_curBallSize + m_growSize);
m_lastPosition = transform.position;
}
public void SetBallSize(float ballsize)
{
m_curBallSize = ballsize;
transform.localScale = Vector3.one * ballsize;
}
Now the problem occurs when the ball enters a scorezone, the scorezone has a 2d trigger which is used to reset the snowball, but unfortunately it just doesn't fire:
//Code used with ScoreZone
void OnTriggerEnter2D(Collider2D other)
{
if (scoreManager)
{
//All balls inherit NormalBall class
NormalBall b = other.transform.GetComponent<NormalBall>();
if (b)
{
Debug.Log(b.name + " hit goal, resetting it");
//Process scoring
scoreManager.AddPoints(Player, b.scorePointValue);
b.BallScored(this);
TheScore.Instance.Score += 12;
//Reset afterwards
destroyObject = b.gameObject;
Invoke("LateDestroy", 0f);
}
}
}
When I remove the scaling portion from SetBallSize(); then the trigger works fine and the ball is reset.
Edit: I should mention that the SnowBall inherits NormalBall, which absolutely works fine with the trigger, leading me to believe it is definitely the scaling screwing something up.
Is there a bug or an issue with scaling a 2d rigidbody/collider against a 2d trigger? Or is it just a thing that isn't supported?
Im a bit confused by this.
$$anonymous$$aybe the scaling causes entering the ball into the trigger, so OnTriggerEnter2D is not fired.
Try to change it to OnTriggerStay2D.
But it's just a quick guess, so I might be wrong.
I don't see any reason it shouldn't work. Scaling should also scale the CircleCollider2D so everything should work properly. Odd that it works when you disabling the actual scaling. Perhaps you should try to make sure that the collider isn't missing the trigger on the Z axis (even though technically it should be more likely to hit since you are scaling the Z axis bigger as well). It could be something weird like getting a negative scale which might look visually ok if you just have a white ball but the collisions might get screwed up. Pause the game when it gets into the scorezone and see if the two colliders are actually inside each other and make sure you do not have any negative scaling.
Answer by Naphier · Jan 05, 2016 at 06:04 PM
I had a similar issue with our last game. We'd scale a gem to make it look like it was shrinking as it got near the center of the goal and sometimes the scaling would mess up the physics, sometimes the triggers. I think the issue may lie in the physics engine not recalculating the collision detection areas as quickly as we'd like it to. I ended up using a combination of continuous detection on the rb as well as OnTriggerStay. The OnTriggerStay gets called many more times than on enter. So I just had to add logic to the gem's class to record whether the trigger event had been called on it or not (also modifying it when it leaves), Hope this helps!
I switched OnTriggerEnter for OnTriggerStay for testing purposes and it worked as normal.
I do believe it is a bug with the 2d trigger/physics system, but due to the way i'm using the trigger there are no ill effects since i am just using it as a fireonce trigger.
shrug
Your answer
Follow this Question
Related Questions
Moving many 2D triggers is slow. Why? 1 Answer
OnTriggerExit2D Does not work 1 Answer