Search Unity

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

Discussion in '2D' started by akim330, Sep 21, 2022.

  1. akim330

    akim330

    Joined:
    Jul 29, 2021
    Posts:
    6
    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:



    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.



    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:



    Here is the code that triggers the swing animation.
    Code (CSharp):
    1.  
    2. public class ItemUser : MonoBehaviour
    3. {
    4.     // _itemParent is an empty game object that is the parent of the axe
    5.     [SerializeField] private GameObject _itemParent;
    6.    
    7.     [SerializeField] private Animator _animator;
    8.  
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.        // Check if character is mid-swing-animation
    13.         AnimatorClipInfo[] clipInfo;
    14.         clipInfo = _animator.GetCurrentAnimatorClipInfo(0);
    15.         if (clipInfo[0].clip.name != "skin_swing")
    16.         {
    17.             // If not in mid-swing, axe parent should be inactive
    18.             _itemParent.SetActive(false);
    19.  
    20.         }
    21.         else
    22.         {
    23.            // If in mid-swing, axe parent should be active
    24.             _itemParent.SetActive(true);
    25.  
    26.         }
    27.  
    28.         // If mouse button is clicked, start swing animation
    29.         if (Input.GetMouseButtonDown(0))
    30.         {
    31.             _itemParent.SetActive(true);
    32.             _animator.SetTrigger("swing");
    33.         }
    34.  
    35.     }
    36. }
    37.  
    I'd really appreciate it if anyone has any ideas for what I could try to fix this. It's totally stumped me!
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    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.
     
    akim330 likes this.
  3. akim330

    akim330

    Joined:
    Jul 29, 2021
    Posts:
    6
    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
     
  4. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    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.
     
    akim330 likes this.
  5. akim330

    akim330

    Joined:
    Jul 29, 2021
    Posts:
    6
    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 :)
     
    Unrighteouss likes this.