Search Unity

Handling forward speed, turn speed, strafe speed in animation?

Discussion in 'Animation' started by Macro, Aug 10, 2014.

  1. Macro

    Macro

    Joined:
    Jul 24, 2012
    Posts:
    53
    I am doing some prototyping with mecanim and am a bit confused as to how to get all of the animations to work together with the given variables available.

    So I have variables forwardSpeed, turnSpeed, strafeSpeed. Forward can be +/- with - meaning walk backwards, + meaning walk forwards and if its > 5 then run dont walk. Then I have turn speed which can be +/- with one being left turn, one being right turn then finally same sort of thing with strafeSpeed.

    So how can I animate them all together, as I have animations for almost all interactions, walking, running, walk_l_strafe etc, but I can only get 2 vars in blending states so can anyone offer advice on how to get this all working together?
     
  2. TMPxyz

    TMPxyz

    Joined:
    Jul 20, 2012
    Posts:
    766
  3. Macro

    Macro

    Joined:
    Jul 24, 2012
    Posts:
    53
    I am trying this currently, but 2d blending trees only allows 2 variables, so I can use forward and turn or forward and strafe... or strafe and turn but not all 3 together, so this is why I have the question, although I did find the documentation useful.
     
  4. TMPxyz

    TMPxyz

    Joined:
    Jul 20, 2012
    Posts:
    766
    Oh, sorry, my mind is a little dizzy just now.

    but you have to use some logic to decide to turn or strafe, right?
    use that value to make a two layer blend tree?
     
  5. Macro

    Macro

    Joined:
    Jul 24, 2012
    Posts:
    53
    well this is the question I guess, as I am open to having the logic being whatever is best, but I have the variables available:

    - TurnSpeed
    - ForwardSpeed
    - StrafeSpeed

    I am assuming that all 3 could happen at the same time, you could be running forward while strafing left while turning right. Some examples I have seen do not have a turn speed, however what if a player is strafing and turning at the same time without moving forward etc...

    So as no tutorial or article I have seen seems to bring all these 3 together I am just wondering if there is a better way to express this movement logic/animation in mecanim, as just because the data is there I do not need to use it all, I just do not know which way is best to satisfy the requirements.
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,708
    Layer your blend trees. My "Normal Locomotion" blend tree is 3-4 levels deep, with motion fields only at the leaves of the tree. This lets you blend on as many parameters as you want.

    For idle and walking forward, it even blends on additional parameters Femininity and Swagger to give a little character to the characters. (Femininity 0=stereotypical male stance, 1=stereotypical female stance; Swagger 0=plain walk, 1=strutting)
     
    TMPxyz and Macro like this.
  7. Macro

    Macro

    Joined:
    Jul 24, 2012
    Posts:
    53
    Are there any articles on doing more advanced animation implementations? as I really like the idea of blending lots of things together (via layers, multiple linked blends etc), but the main tutorials are walking -> running, and granted for people just starting out thats fine but when you get into more serious stuff there is a lack of information (well from what I could find online).

    As its all well and good you saying that you have multiple layers all blended together but can you provide a screenshot or something which shows how you have hooked it all up and the kind of interactions between the blended sections etc? As one thing (other than the handling of multiple vars) I am struggling with is blending things together nicely, i.e if you have a normal walk, a swagger and are injured that could be 3 blended animations (I am just making this up), and knowing how to blend an almost inifinite amount of animations together would be brilliant to nail.
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,708
    I'll post a screenshot when I get back to the office. Crouching and injury are handled on a higher, additive layer.

    I haven't seen many articles on advanced Mecanim. Back in 2012, Heikki Tormala wrote a good article about how they used Mecanim in AirBuccaneers. They used an early version of Mecanim without some of the conveniences we have now. They did an impressive job building some huge, convoluted state machines. Nowadays it would be split into more manageable sub-state machines.

    If you have access to ooti's Motion Controller or Robin Schmidt's Character System, they contain some fairly involved animator controllers. If nothing else, you could glean some ideas from their online documentation.
     
  9. Macro

    Macro

    Joined:
    Jul 24, 2012
    Posts:
    53
    I have the character system, it seemed to be VERY convoluted and I was not sure if it was quite the best practices when it came to certain animations, however you are right it will at least be something to look at. Will look into the motion controller too, but a screenshot showing me how your stuff hooks together would be very useful, I am happy to pm you my email address if you dont want to post your images publicly.
     
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,708
    Here are a couple screenshots. I chopped this out of a larger animator controller to take some screenshots without all the extra stuff. The original animator controller has these layers:
    • Base Layer: Whole body animations
    • Arm Layer: Upper body animations
    • Aim Layer: Additive layer to blend aiming in all directions (but not used in Pro, where I use IK)
    • Posture Layer: Crouching, limping, etc.
    • Reaction Layer: Hit reactions
    • (The head is controlled by a legacy Animation component because it uses some FaceFX scripts for facial expressions and lipsync.)
    The top level of the Base Layer looks very simple. It just contains one state machine per stance (Unarmed, Pistol, Rifle, etc.).

    When you drill into a stance state machine, it has a blend tree for normal movement, another for movement in cover, and other states for things like vaulting, diving (this is actually a blend tree, too, to support diving at angles), jumping, falling, etc.

    An older version of this controller had a mess of transitions. I now use Animator.CrossFade() heavily, which enabled me to get rid of most of the ugly transitions.

    The control method doesn't use turning-left and turning-right animations. Instead, the player rotates with mouse movement. It looks fine when the player is moving and feels very responsive. When the player is stationary, the Stationary sub-blend tree uses in-place rotate-left and rotate-right animations.

    The parameters are:
    • forward: -1 = run backward, +1 = run forward
    • lateral: -1 = run (strafe) left, +1 = run right
    • rotation: -1 = maximum in-place rotation to the left, +1 = max to the right
    • femininity: 0 = male, 1 = female (used for idle and walking forward)
    • swagger: 0 = plain, 1 = lots of swagger (used only for walking forward)


     
  11. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,214

    Thank you for posting this; but how do you blend in all the other parameters (such as rotation) given that the blendtree only allows two be blended? In the screenshot above, you're blending lateral and forward, so how do you handle rotation?
     
  12. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,708
    Nested blend trees. In the picture, you can see the parameter(s) that each sub-blend tree works on.

    Side note: In the animator above (wow, all the way back in 2014), I only animated rotation while standing still. While moving, the rotation was controlled by a player input script or NavMeshAgent. This makes the player character more responsive, even if it's less physically realistic. It was made for a third person shooter, so responsiveness was more important.
     
  13. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,214
    A search for nested blend trees didn't find any explanation of how to set one up, and I couldn't find any option to add multiple blend trees (I'm using 5.0.1 Personal). Where is this explained?
     
  14. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,708
    Hi - Inspect your blend tree. In the Motion section, click "+" and select "New Blend Tree". This will create a child Blend Tree:

    nestedBlendTree.png
     
    HonoraryBob likes this.
  15. HonoraryBob

    HonoraryBob

    Joined:
    May 26, 2011
    Posts:
    1,214

    Thank you.
     
  16. nikiataei

    nikiataei

    Joined:
    Nov 4, 2021
    Posts:
    1
    I know this is very late but can you share your player input script? I am having a similar problem. I am using WASD for movement direction and my mouse for rotation but I am having trouble with the rotation as I use velocity.x and velocity.z to control my animations. But with these parameters my rotation is not playing the correct animations based on my rotation.
     
  17. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,708
    That was so long ago that I don't even remember what project it was. Looking at the blend trees above, this project wasn't using turning animations. It just rotated the character's transform to face straight ahead. You could use some time-based damping to change the rotation gradually, but I prefer immediate rotation to prevent it from feeling sluggish.