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

Choose next animator state from script impossible?

Discussion in 'Animation' started by RakNet, Jul 29, 2017.

  1. RakNet

    RakNet

    Joined:
    Oct 9, 2013
    Posts:
    313
    I have a character which needs to choose one of many possible attack animations. Which one he chooses depends on the path the animation clip takes, which I read using animationClip.SampleAnimation(...). He chooses the animation whose path brings the attack animation most close to the hit location on the target.

    So if the enemy is on the ground he will do a low swing, if the enemy is far he does a lunge, etc.

    So how do I pick which clip to play from script? I've been looking for the last two hours and as far as I can tell you cannot.

    Problems:
    1. Animator.Play() takes the name of the Animation state, not the clip.
    2. I can't do that anyway because there is no way to enumerate the list of animation states during runtime
    3. Even through I can read the names of all the clips, I don't know which clips are attack or not. This is setup in the Animation controller as a tag on the animator state, which as I just said I can't enumerate.
    4. Even if I could do all of the above, I have no way to read which animator states are valid transitions from the current state.
     
  2. RakNet

    RakNet

    Joined:
    Oct 9, 2013
    Posts:
    313
    So I came up with a semi-solution of indicating the clip name in an animation parameter. This isn't ideal because it means the transition depends on the clip name and not the state name as it should.

    BUT even that doesn't work because you can't pass strings as a parameter. So my next idea was to hash that string and pass that as an int parameter. This is even more bad because it requires the artist working with the animation controller to know what string hashes are. BUT that doesn't work either because internally the parameters are floats, so break with large INT32 numbers.

    Now I am passing the clip names as int parameters, ranked from 0 onwards to determine which attack is the next best attack to run. Of course there is no way to say "Give me the lowest of all these parameters" other than a lot of duplication, but it should work in the end.

    Are there plans in the future to have a replacement for Mechanim? I looked in the asset store but didn't see anything.
     
  3. NoiseFloorDev

    NoiseFloorDev

    Joined:
    May 13, 2017
    Posts:
    104
    You can get at this info from editor scripts with AnimatorController, so I'd write a script to go through the graph and find the info I need, then write it out to a file asset I can read at runtime.