Search Unity

An animation clip glitch I’ve introduced that I don’t understand

Discussion in 'Animation' started by chris_gamedev, Aug 6, 2021.

  1. chris_gamedev

    chris_gamedev

    Joined:
    Feb 10, 2018
    Posts:
    34
    Hey

    I’ve been coding up some FPS mechanics for an asset I’m working on. I had already wired up most of the animations and recently added weapon switching and a holster animation.



    Everything was working fine as seen in the first part of the video.

    The console logs display which animation clips are being played.

    After the gun is holstered and redrawn the shoot animation goes funny.

    I know there’s lots of variables here and I can provide more info as needed but does anything jump out at you or have you seen anything similar before?

    Thanks
     
  2. chris_gamedev

    chris_gamedev

    Joined:
    Feb 10, 2018
    Posts:
    34
    I thought I should add some context now I've looked into it further.

    This is the animation states I have wired up; I haven't done this in the typical way that is taught in tutorials (generally using conditions to trigger states) because whenever I've tried that in the past it produces sub-par results:

    upload_2021-8-7_20-10-50.png

    It's fairly simple to understand - and as you can see it mostly works - the Entry state transitions to a state I've named "null_state". It doesn't an animation clip assigned.

    All of the clips are triggered using e.g.
    animator.Play("draw_not_empty")
    . All of the clips transition to the appropriate idle state.

    The exception to this is the holster states. We don't really want to transition to anything here because at the point when the holster animation is finished, the gameobject is deactivated and in future would be reactivated presumably from Entry -> null_state then whatever animation is played next.

    FWIW I've tried numerous things; transitioning to default, transitioning to idle etc.

    Interestingly, transitioning to idle fixes the issue though the holster animation finishes and flashes the weapon on screen for a frame because that's what the idle animation is.

    The holster animation is the issue; if I remove that animation from the equation entirely (just hiding the weapon and activating the next) when the weapon is activated subsequently the shoot animation is normal.

    Is anyone able to explain to me (preferably like I'm 5) what is going on here? It feels like a bug in Unity but happy to change approach or implement a workaround if there is one.

    Many thanks for any input!
     
  3. atcarter714

    atcarter714

    Joined:
    Jul 25, 2021
    Posts:
    10
    What does you hierarchy of the player and gun objects look like? I had some problems with that as well. I also had to be careful to copy keyframes from my "idle" gun animation into other animations so they could connect seamlessly without twitching. What's going on here sure is odd though, and I think more info is needed to diagnose the problem.

    Also, what is that "null_state"? Is that just a totally unanimated state with a zero transform?
     
  4. chris_gamedev

    chris_gamedev

    Joined:
    Feb 10, 2018
    Posts:
    34
    The hierarchy can be seen in the video. Player is where the player stuff and cameras are, plus a container for the weapons which has the switcher script, followed by the weapon game objects. The pistol object is a parent container where the weapon script is, and its children are the mesh/bones etc.

    I’ll attempt to do some debugging of the animation clip itself in case there’s some weird key frame issue between the two scripts.


    null_state is just an empty state with no animation clip assigned because I don’t want any animations to fire when the game object is activated. Instead I trigger the appropriate draw animation clip in code which then transitions to the appropriate idle state.
     
  5. chris_gamedev

    chris_gamedev

    Joined:
    Feb 10, 2018
    Posts:
    34
    I'm not entirely sure why this works but adding an OnDisable method sorts out the issue.

    Code (CSharp):
    1. private void OnDisable()
    2. {
    3.     _animator.Rebind();
    4.     _animator.Update(0f);
    5. }
    The switching works by deactivating the gameobject so presumably this is a good way to entirely reset the animator so that when the gameobject is later reactivated, it's starting from its original state.

    After spending too long on this, I don't care; I'll take it!
     
    atcarter714 likes this.
  6. atcarter714

    atcarter714

    Joined:
    Jul 25, 2021
    Posts:
    10
    Hmmm, why not? I'll have to remember that if I ever have any similar problems. If there's any reason to do so you might be able to figure out the actual cause and solution in the future. If you do, be sure to post here and let us know. Animation drives me freakin' nuts sometimes!
     
    chris_gamedev likes this.
unityunity