Search Unity

Question Change Animation State via Code at Run-time

Discussion in 'Scripting' started by local306, Jan 29, 2021.

  1. local306

    local306

    Joined:
    Feb 28, 2016
    Posts:
    155
    Anyone know of a way to change for example the Mirror parameter of an individual state at run-time via code without having to create an explicit bool? Like basically alter AnimatorState.mirror but independent of the UnityEditor namespace (since you can't build with that).

    I have a pool of idle animations and it would be nice to make them a little more varied by being able to crossfade into one another but have like a 50% chance of being mirrored or not.
     
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Can just make the code that triggers the state to begin with do a random to find which state it will choose and in the animator you would set up transitions between all the different states that could be chosen.
     
  3. local306

    local306

    Joined:
    Feb 28, 2016
    Posts:
    155
    Hoping to avoid this as I don't understand why there isn't a code API to handle this programmatically. Like all of the states in the idle animation controller are only called using their animation clip name since I can randomize all of the sequences far more easily in code versus having to set up transitions away from everything.

    To me it's a huge missed opportunity not being able to access the states themselves at runtime for an animation controller.