Search Unity

Animator finding the original clip info

Discussion in 'Scripting' started by kinguhdahood5, Apr 17, 2021.

  1. kinguhdahood5

    kinguhdahood5

    Joined:
    Dec 18, 2018
    Posts:
    84
    Is there a way to find the original animation in an animator override controller instead of the substituted clip? For example, in the original animator, a clip is called "Special", but in my override animator, the clip name is "ShootCannon". If I have the override controller as the animator, and the "ShootCannon" animation is playing, is there a way to return the original animation name as "Special"?

    I am using this piece of code here:

    Code (csharp):
    1.  
    2. string currentClip = anim.GetCurrentAnimatorClipInfo(0)[0].clip.name;
    3.  
    Which returns the current clip that's playing, but my goal is to return the original clip name that the override clip is playing. So if the substituted clip is "ShootCannon", "ShootGun", "SwingHammer", etc, and that animation plays, it will return "Special". Is there a way to access that?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    There might be a way to do this but this is a fairly problematic approach.

    Instead you should change your game logic so that it knows what it wants and is not reliant on the names of assets that happen to be used to present what is going on.

    The reason is: if in the future you change how the assets are laid out, the underlying game logic should NOT have to change.
     
    kinguhdahood5 likes this.
  3. kinguhdahood5

    kinguhdahood5

    Joined:
    Dec 18, 2018
    Posts:
    84
    That's a fair point. I had problems with the original way I was coding my animator, using variables and whatnot to determine the attack or special, so I turned to getting the animator clip info, which works right now but yeah, I would have to change multiple things each time I enter new info. The original problem was that I would set an animator bool to true, wait a frame, and set it to false while giving it a cooldown, but sometimes the animator would miss it and not play the attack.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Generally you want to architect your game logic so that it would work even if you disconnect the presentation of the game, eg., objects, animations, etc.

    The only exception would be an engineered solution such as AnimationEvents, but even those suddenly impose a requirement that each animation implement the appropriate callbacks. Any animation missing events or event handling would necessarily break the entire game, so you want to box up that into a tightly-testable small area of the code and assets, not scattered all over.
     
    kinguhdahood5 likes this.
  5. kinguhdahood5

    kinguhdahood5

    Joined:
    Dec 18, 2018
    Posts:
    84
    Alright, I did some rework, went back to the original way I did it except with a trigger event, and it works now. I see what your saying though, relying more on the basic code to do the work, I'll keep that in mind going foward. Thanks for the tip
     
    Kurt-Dekker likes this.