Sprite animation (moving transform) works fine in the editor but is messed up in play mode

I have a simple axe-swing animation. I recorded moving the axe transform along with the hand of the character while in a swing animation. When I play the animation in the editor, it looks perfect:

cu4c3u

However, then when I enter play mode, the axe swing looks totally messed up. The axe is no longer on the hand. When I look in the inspector with the axe selected, its local position values are totally different from the ones I set.

gpz3gw

I’ve searched a while for an answer to this problem. I’ve set all keyframes to Both Tangents → Constant as was the solution for some people for a messed up animation.

Here is a recording of my animator as I am swinging the axe:

jg92pp

Here is the code that triggers the swing animation.

public class ItemUser : MonoBehaviour
{
    // _itemParent is an empty game object that is the parent of the axe
    [SerializeField] private GameObject _itemParent;
   
    [SerializeField] private Animator _animator;

    // Update is called once per frame
    void Update()
    {
       // Check if character is mid-swing-animation
        AnimatorClipInfo[] clipInfo;
        clipInfo = _animator.GetCurrentAnimatorClipInfo(0);
        if (clipInfo[0].clip.name != "skin_swing")
        {
            // If not in mid-swing, axe parent should be inactive
            _itemParent.SetActive(false);

        }
        else
        {
           // If in mid-swing, axe parent should be active
            _itemParent.SetActive(true);

        }

        // If mouse button is clicked, start swing animation
        if (Input.GetMouseButtonDown(0))
        {
            _itemParent.SetActive(true);
            _animator.SetTrigger("swing");
        }

    }
}

I’d really appreciate it if anyone has any ideas for what I could try to fix this. It’s totally stumped me!

Only thing that immediately comes to mind is that your transforms might be causing it. Make sure the “Player” and “Weapon” transform components have rotation at 0,0,0 and scale at 1,1,1.

This is a very simple setup, so something has to be messing it up somewhere. Hard to tell from the video though.

I highly doubt it’s a bug, but if you share what version of Unity you’re using, I can double check that.

1 Like

Thanks for the reply! My “Player” and “Weapon” transforms do have (0,0,0) for rotation and (1,1,1) for scale so it seems like that’s not the problem as long as something unexpected isn’t happening at runtime? But I don’t think that’s the case.

I also would be surprised if it’s a bug but the version I’m using is 2021.3.5f1

I just tested your setup with 2021.3.5f1, and I can’t repro the behavior (didn’t test the script). Something must be messing with the transform at runtime. I’d recommend rebuilding the player with the animation, testing frequently, and observing at what point the animation breaks. Then you’ll know what’s causing it.

1 Like

Thank you for trying to reproduce it. I really appreciate your help! I think you’re right that there must be some other thing that’s messing with the transform at runtime. I’m going to try rebuilding the whole setup and see where it goes wrong.

Thank you :slight_smile:

1 Like