So, I have an animator , i can access it. But from there I have no idea how to access the clips inside. I need to access the transition of the clips.

I got as far as accessing the object of the "Animator" class.

enter image description here

EDIT:

It appears The TransitionInfo datatype doesnt provide conditions. I need the conditions!

enter image description here

  • Are you trying to access it via code or in the inspector? – aytimothy Dec 2 at 5:30
  • via code of course :) – OC_RaizW Dec 2 at 11:32
  • I'm curious why you need to access the transitions themselves. Can you describe what feature this is serving? There might be other ways to achieve your ultimate goal. – DMGregory Dec 7 at 10:44
  • @DMGregory: Its about the transition. You see, during the transition the animator is in 2 states at once. And setting the transition time to 0 doesnt help. He is always at least for a very short time in 2 states, which messes with "on state enter" and "on state leave"- since LEAVE is called before enter. – OC_RaizW 2 days ago
  • Can you describe how this causes issues in your game? We might be able to find more straightforward solutions to those issues. – DMGregory 2 days ago

It's somewhat complicated but achievable. This is a list of things you'll need to go through:

  1. Give the transition a name in the editor. Let's say you call it "AnimAToAnimB".

  2. Figure out which layer of the animator the transition is in. Let's assume it's in the default "Base Layer".

  3. Query the hash code of the transition up-front via code:

    animator.StringToHash("Base Layer.AnimAToAnimB");

    and store it in some variable, let's say transitionAToBHash.

  4. Research "Animator State Machine Behaviours". You'll want one attached inside the layer of the transition that you're interested in.

  5. Implement your own custom StateMachineBehaviour that will be able to listen and receive animator state change callbacks. More info: https://docs.unity3d.com/ScriptReference/StateMachineBehaviour.html

  6. Once you have your custom state behaviour attached and implemented, upon receiving the OnStateExit callback, grab the currently active animation transition and compare it to your cached transition hash:


// inside your custom state machine behaviour

override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
   var transitionInfo = animator.GetAnimatorTransitionInfo(layerIdOfYourTransitionAToB);

   if (transitionInfo.userNameHash == transitionAToBHash)
   {
      // PROFIT! your transition "transitionAToB" has just started!
   }   
}

Note: it can get a lot more complicated if you're using things like sub state machines.

  • sir youre awesome. I actually gave up a while ago... I thought its unachiveable since I read a 3-pages long forumpost about this particular problem. – OC_RaizW 2 days ago
  • 1
    Whops... i have to take the "correct answer" back... I mean. Yeah i get the info, but i dont have access to those conditions inside that particular transition. – OC_RaizW 2 days ago
  • 1
    Can you provide more details as to what do you mean by "I don't have access to those conditions"? – mt_ 2 days ago
  • Allright, i updated my question – OC_RaizW yesterday
  • In theory it's still doable, but the solution wouldn't be pretty nor very maintainable. You'd have to write an editor script that would grab your AnimatorController, walk all of it's transitions, as shown here: forum.unity.com/threads/find-all-animator-transitions.473779, access the conditions inside AnimatorState.transitions and then create a dictionary that would map transitionHash -> yourOwnConditionStruct and eventually pass that dictionary to a game object that's interested in knowing transition conditions. Also, you'd have to run this script every time transitions change. – mt_ yesterday

Use the below code to call the animation using script

public Animator Anim;
void Start(){
    Anim.Play("Animation_Clip_Name");
}
//Trigger it by button click or inside other functions to change the animation.
public void NextAnimation()
{
   Anim.Play("Next_Animation_Name");
}    
  • yeah thats not the question here – OC_RaizW 2 days ago

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.