Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animation Control

Discussion in 'Scripting' started by ricardoaffonso, Mar 21, 2018.

  1. ricardoaffonso

    ricardoaffonso

    Joined:
    Feb 21, 2018
    Posts:
    17
    Hi,
    I am not very experienced in unity so please excuse me if the solution is obvious, but I researched and tried quite a few things before coming here.

    I have a bird object that is coded to be doing one of four things: flying, landing, taking off, or sitting idle.

    I have a switch statement inside the update function that detects which state it is in

    Code (CSharp):
    1.      
    2.             case 0:
    3.                 flyingUpdate();
    4.                 break;
    5.             case 1:
    6.                 break;
    7.             case 2:
    8.                 break;
    9.             case 3:
    10.                 timeLanded -= Time.deltaTime;
    11.                 lookForward();
    12.                 break;
    13.             default:
    14.                 break;
    What I want to do is change the animation in the state machine based on what the bird is doing
    Is there anyway I can do this through code inside the switch statement cases?
     

    Attached Files:

  2. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    You need to solve this on the AnimatorController. Check blendTrees and assign a float parameter State to it. Then from code set the parameter and it should work.

    Code (CSharp):
    1. animatorController.SetFloat("State", state);
     
    Lethn and ricardoaffonso like this.
  3. ricardoaffonso

    ricardoaffonso

    Joined:
    Feb 21, 2018
    Posts:
    17
    Problem with that is blend trees are fro clips, I want to be able to change the state so it's not just a short animation that happens but a full cycle of sub-states animations
     
  4. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    You can have trainsitions with State ">0" ">1" ">2" ">3" etc from a state machine to another one. Simplest way I can think of.
     
    Lethn and ricardoaffonso like this.
  5. ricardoaffonso

    ricardoaffonso

    Joined:
    Feb 21, 2018
    Posts:
    17
    I'm not sure what you mean, but I actually solved it by making clips out of the animations and using them on loops.
    Thank you for the help
     
    whileBreak likes this.
  6. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    I'm guessing you exported from 3Ds or maya or something, where all animations are done on just one clip?
     
  7. ricardoaffonso

    ricardoaffonso

    Joined:
    Feb 21, 2018
    Posts:
    17
    Yep precisely
     
    whileBreak likes this.
  8. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    ok great, that's the way to do it