Download

Dynamically changing AnimationTree at runtime

Hello guys!

I am adding a cool feature on the gameplay of my game, on which the enemies get hurt. I have got many injured mocap animations, I already edited/fixed them in blender and retarget these anims to UT3 skeleton, which is what I am using for all my chars, for the sake if simplicity, however I am having some difficulties on applying them in-game.

I know there are many ways to do the same thing in UDK, however I always prefeer to stick to the most easiest of them.

I did study deep about animation trees in UDK on the last days, so I found a good idea of having the injured animations using the AnimNodeBlendByPhysics, because according to UDN:

"This blend node allows the Anim Tree to automatically blend between the inputs which match the owning actor’s Physics variable. This is helpful in the situation where you want characters to animate differently when they’re walking, falling, swimming etc. This blend node is automatic."

The default UDK Animation Tree uses the UTAnimBlendByPhysics, which is very limited, it only have 3 branches, which are Walking, Falling (Jump), and Swimming. So I changed it to the AnimNodeBlendByPhysics which has almost 13 branches.

So I did a search on the UTClasses to try to understand each of these branches, how they are called in code, so then I found that almost all of these branches already have got some function on some classes that calls them, like PHYS_NavMeshWalking, PHYS_Interpolating, PHYS_Rotating.

However I have found 3 branches that I could use, because they are no called by any classes, which are the PHYS_Spider, PHYS_SoftBody and PHYS_Custom. So on these branches I have set the animation nodes to allow the player move with the injured animations (I have for now just 3, which are injured leftleg, injured rightleg and injured chest). I tested all the other branches and no one worked except these 3.

So I have set a simple blend by directional (forward, backward, left and right), and also a idle, and moving node. Now the animation nodes for injured states were setup correctly, and blending perfectly inside the animation tree.

So for getting these animation playing in game, as I already have setup a system for bone detection, so just added a function to change the physics state of my pawn (change to PHYS_Custom / PHYS_SoftBody / PHYS_Spider):


      if(HitBone == 'b_LeftToe' || HitBone == 'b_LeftAnkle' || HitBone == 'b_LeftLeg' || HitBone == 'b_LeftLegUpper')
      {

       TopHalfAnimSlot.StopCustomAnim(0.15);

      `Log("Left Leg Hit Impact Damage");

       FullBodyAnimSlot.SetActorAnimEndNotification(true);
       FullBodyAnimSlot.PlayCustomAnim('hit_left_leg_UT3', 1.0, 0.35, 0.75, false, true);

**       SetPhysics(PHYS_Custom);**

       SetTimer(2.00,false,nameof(HitRecover));

      }      


It worked almost perfectly, the animation changed from normal walking to walking injured. However, there was a problem now, the pawn stopped moving, he would play the animation however would not move from it’s place. So then I understood that this happened because the only physics state on which the pawn can move is indeed PHYS_Walking, so whenever I changed this Physics state, it stoped walking.

So now another solution I found, which worked, is to have a different animation tree for each one of the injured states, like AnimationTreeNormal, AnimationTreeInjuredRightLeg, AnimationTreeInjuredChest, and so on, then just change the animationtree in runtime by using this code after the hitbone detection:


Mesh.SetAnimTreeTemplate(AnimTree'CH_AnimHuman_Tree.AT_CH_Human_Injured_Chest');


This worked because I have set this function for my pawn:


//override to do nothing
simulated function SetCharacterClassFromInfo(class<UTFamilyInfo> Info)
{
  Mesh.SetSkeletalMesh(defaultMesh);
  Mesh.SetMaterial(0,defaultMaterial0);
  Mesh.SetPhysicsAsset(defaultPhysicsAsset);
  Mesh.AnimSets=defaultAnimSet;
  Mesh.SetAnimTreeTemplate(defaultAnimTree);
}

However, the problem is that there is no smoothing whenever changing from one animation tree to another, which looks a bit weird and jerky, whereas whenever I used to change between Physics States the animatons did blend perfeclty and smoothly.

**Does anyone has a better idea of doing this?

Cheers**

Personally, I think I would use an AnimNodeBlendByProperty hooked up to regular and wounded versions of various animations. Then I would give my pawn a variable that tracks percentage of health and make the AnimNode use that variable to blend between regular and wounded animations.

Thanks man for the tip! I will try it.

Cheers :smiley: