OnTriggerEnter2D not fired while OnTriggerStay2D is
when changing child position (using transform.localPosition), OnTriggerEnter2D is not triggered on child object while moving and evidently colliding (even OnTriggerStay2D s triggered) Only colliders are attached to objects, no RIgidbody2D. Triggers are working properly when not changing localPosition in Update().
if (riding == RidingState.MovingRight)
{
transform.localPosition = Vector2.Lerp(leftPos, rightPos, relU);
relU += Time.deltaTime * slideSpeed;
if (relU >= 1f)
{
relU = 1f;
transform.localPosition = new Vector3(rightPos.x, rightPos.y, 0);
riding = RidingState.StableRight;
}
}
Answer by hexagonius · Dec 17, 2015 at 12:11 PM
That's correct behaviour. changing position is teleporting, not moving. use AddForce or change velocity, but do not alter the position. stay is triggered because an overlap exists after moving into another.
But there is no Rigidbody2D on the object, there is one on the parent, but not on the child object, child has just BoxCollider2D as trigger. So there is no way to get Enter and Exit fired purely on collider triggers.. Somehow I missed that in documentation. EDIT: and also I read, that there can't be rigidbody on parent and also child.. it must be then solved through joints..
well, the rigidbody combines its colliders (own and children) for$$anonymous$$g a compound. a script on the rigidbody reacts to all of them. last thing I never did but could work is changing the position of the child in fixed update, but I doubt it works.
I tried moving it to FixedUpdate(), didn't help. And yes, you were right about combining colliders (I put the OnTriggerEnter2D in parent - where Rigidbody2D was), but again didn't work when the child was moving using localPosition (wasn't triggering Enter trigger again), only when it was stable.
But when I tried to put rigid body (turn off gravity) into child and then only set it's velocity to one of parent, everything started working like it should. Oh boy, Unity.