- Home /
Animated sprite's bounds not in sync with its visuals
I'm animating a player character using two different animated child objects ("top" and "legs") each with an animator and sprite renderer. In order to keep them physically connected I have top positioned directly above leg's sprite:
The issue is when the height of legs' sprite changes it takes a frame for legsSR.sprite.rect.height to update, meaning the two sprites either overlap or separate by a pixel. For example, when finishing a jump the legs crouch by a pixel then up again, during which the character looks like this for a single frame:
I've verified that for the first frame the height of the sprite visually changes, the script still reads the previous pixel height of legs' sprite. I've tried putting the animation script at the start and end of the script-execution order, and applying the check in both Update and FixedUpdate.
Relevant code snippet:
private void Update ()
{
// Positions top sprite immediately above the legs
topAnimator.transform.localPosition =
new Vector2
(
0,
(legsSR.sprite.rect.height - legsSR.sprite.pivot.y) / legsSR.sprite.pixelsPerUnit
);
}
My question is twofold:
Am I actually approaching this problem correctly?
If so, how do I ensure an accurate value for the bounds of a changing sprite?