1
\$\begingroup\$

Description:

I am creating a 2.5D RPG.

When my character moves from left to right, they rotate 180 degrees to face the new direction. Just like this example.

enter image description here

They also have a blob shadow sprite that follows their X position.

It's rotated 90 degrees in the X-axis to lay flat underneath them.

enter image description hereenter image description here


Goal:

Because my character is asymmetrical, I came up with the idea of having their shadow extend the same distance past each foot.

(In the example below, each foot is 18 pixels from the edge of the shadow)

This way, even if the character was rotated 180 degrees and the shadow did not move, they would look exactly the same but mirrored. The example below is exactly what I want.

enter image description here enter image description here


Issue:

My problem is that once my character rotates 180 degrees, they're positioned very differently on their shadow. Which makes sense due to their asymmetrical shape. I believe the issue here is the origin/pivot point being used to rotate the character.

enter image description here enter image description here


What I've Tried:

To resolve this I tried creating a parent object for my character's sprite. That way I could set an offset in the X-axis to sort of "fake" changing the pivot point.

The Hierarchy looks like this:

enter image description here

Though, it still doesn't look good and is far from precise because I'm just making rough estimates of how much of an offset to use. Is there a formula that can be used to figure this out?

If I'm going about this the wrong way, I am completely open to a different way of achieving this.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Why is the shadow not a child of Character as well? And why do you need to fake the pivot point and can't just set it in the sprite editor? \$\endgroup\$
    – Zibelas
    Mar 11 at 6:44
  • \$\begingroup\$ Thanks for the reply! The shadow could be a child of the character game object. As for setting the pivot point, I'm actually using a Spine animation rig. So, I can't use the sprite editor unfortunately. Though, thinking about it, even if I could use the sprite editor I still wouldn't know where to place the pivot point ^^; \$\endgroup\$ Mar 11 at 12:32

1 Answer 1

1
\$\begingroup\$

Cache the shadow's authored horizontal offset from the sprite either at edit time or on spawn:

Start() {
    shadowOffset = shadowTransform.position.x - spriteTransform.position.x;
}

Each frame during a rotation, apply that offset or its mirror image, according to the rotation of the sprite:

void Update() {
    // 1x for 0°, -1x for 180°, smoothly varying in between.
    float side = Mathf.Cos(rotationAngle * Mathf.Deg2Rad); 

    var shadowPosition = shadowTransform.position;
    shadowPosition.x = spriteTransform.position.x + side * shadowOffset;
    shadowTransform.position = shadowPosition;
}

This assumes your shadow's pivot is in its center, which shouldn't be too onerous.


If you'd rather keep the shadow in-place and rotate the character inside it:

  1. Create a new empty child under the shadow. Ensure it's centered on the shadow. Call this "Pivot Parent".

  2. Drag this parent out from under the shadow, to be a sibling to the character's body object in the transform hierarchy. Set its z depth to the same as the character body's.

  3. Drag the character into this parent object. Unity will by default adjust its local position inside the parent to maintain its world position unchanged.

  4. Change any scripts that rotate the character body to rotate "Pivot Parent" instead. Since you arranged it to have its pivot point centered on the shadow, this will keep the character in the right relationship as it rotates, even if it's shifted off-center to some degree. If the shadow was following the character body in a script, change it to follow the pivot parent instead.

\$\endgroup\$
4
  • \$\begingroup\$ Thank you very much for putting in so much effort! While this seems like a fantastic route, unless I'm misunderstanding, this moves the shadow to the correct position, correct? I'd like the shadow to stay in the same position and only rotate the character so that it appears mirrored. \$\endgroup\$ Mar 11 at 17:34
  • \$\begingroup\$ That really sounds like you want to change the character pivot then, so they pivot around the center of the shadow, or use the RotateAround method to rotate around a point in the center of the shadow regardless of where the character's own pivot is. \$\endgroup\$
    – LudoProf
    Mar 11 at 20:10
  • \$\begingroup\$ Right, exactly! The pivot is what I need to change here. I'm just lost on where to actually place the pivot and if the way I'm using an offset is the best way. As for using the RotateAround method, I'm not really sure how I can use it to get the results that I'm looking for. \$\endgroup\$ Mar 11 at 22:19
  • \$\begingroup\$ @PayasoPrince - updated; does that help? \$\endgroup\$
    – LudoProf
    Mar 21 at 17:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .