• RuntimesUnity
  • WorldRotationX not reflecting character flip (scale.x negative)?

I'm working on a 2D game with a character that shoots a gun.
I'm using Spine, and I'm trying to get the world position and rotation of the gun muzzle bone (e.g., ShotPos) to spawn bullets in the correct direction.

Using bone.WorldX / bone.WorldY and converting them with transform.TransformPoint() gives me the correct position in world space.

However, when I use bone.WorldRotationX to get the rotation (Z-axis), it doesn't change when the character is flipped (scale.x is negative).
In other words, the character visually faces left, but the rotation still behaves as if it's facing right.

How can I get the visually correct rotation of the bone when the character is flipped horizontally?

Related Discussions
...

When you flip a character by setting scaleX to -1, the WorldRotationX of a bone does not automatically account for this flip in terms of visual direction. This is because WorldRotationX is calculated based on the bone's local rotation and its parent's world rotation, without considering the sign of the scale. To get the visually correct rotation when the character is flipped, you need to manually adjust the rotation based on the scale. If scaleX is negative, you can subtract 180 degrees from the WorldRotationX to get the correct visual rotation. This adjustment accounts for the horizontal flip.

float visualRotation = bone.WorldRotationX;
if (skeleton.ScaleX < 0) {
    visualRotation = 180 - visualRotation;
}

This code snippet adjusts the rotation to reflect the visual direction of the bone when the character is flipped.

  • Vein ответили на это сообщение.
  • Vein оценил это.

    Spinebot
    Thanks! I finally got it working! The rotation was tricky, but adjusting it based on scale.x did the job. Really appreciate the explanation.