In this part of the tutorial series, we will set up the animation controller for the hands that we imported in the previous post. We will set up the animation blueprint so that we can easily add in the input functionality from the motion controllers to do the grab action on the hand when the trigger buttons are pressed. Input handling is the topic for the next post.
Import the Animation Assets:
The first thing that we need to do is to import the animations for the hand from the VR template project into our own project. This is done exactly like we did with the hand mesh in the previous part of the tutorial. The assets that we are interested in are in the “Content\VirtualReality\Mannequin\Animations” folder. We will migrate the “RightGrip_BS” asset from the folder. This is the blend shape that has been set up to use the animations that we are interested in. When you attempt to migrate the assets, you will find that the engine will pick up the material and the meshes that have already been migrated it. You can choose to overwrite or skip. Since we already have them in the project, it does not matter which you choose.
Let’s start by opening the blend-shape file that we have now imported into our project. We should be able to see that the blend-shape is set up such that the blend value of 0 will have the hand in a completely open state, 0.5 will have the hand in a state which indicated that an object can be grabbed and the value of 1 shows the hand in the grabbed state. We will now start by creating the animation blueprint that will start using the blend-shape set-up.
Setting up the Animation Blueprint:
We will start by creating a new animation blueprint from the hand skeleton. This process is shown in the set of screenshots below. Let’s call this animation blueprint file “RightHand_AnimBP“.
Once we have the animation blueprint created, let’s open it up and set it up to use the blend shape. we start by creating a state machine and hooking this up to the Final Animation pose. Let’s call this state machine “GrabStateMachine“. Let’s open up the “GrabStateMachine” and create the variables that we will require to get the animations working with the input from the controllers.
First, we create 2 boolean variables called “bCollidingWithGrabbable” and “bTriggerIsHeld” for this tutorial we will only be using the trigger held variable to check for the input from the controller. In future series, where we will look into the aspects of teleportation for movement and using the input from motion controllers to interact with objects in the scene is where the colliding with grabbable will be useful. Lets set the default value for both to be false for now.
Create Animation State Machine for Grab Actions:
We will now create 3 states within the state machine for Open, CanGrabObject and IsGrabbingObject. Entry to the state machine will be hooked up to Open which will be hooked up to CanGrabObject and that will be hooked up to the GrabbingObject state. The transition from Open happens when the “bCollidingWithGrabbable” variable is set to true. The transition from Open state to IsGrabbingObject happens when both the booleans are set to true. The state machine set-up and the transitions are illustrated in the screenshot below.
Code to Add the Animation Blueprint to Hand:
Next, we will need to set the animation blueprint to the skeletal mesh that we have created in the previous post to display the hand mesh. To do this we will first create a function that will load the animation blueprint that we have just created and assign it to both the hands. Let’s call this function “SetHandAnimationBlueprint“. In the header file of the player pawn we add the declaration as follows:
void SetHandAnimationBlueprint(USkeletalMeshComponent* a_refHand);
Next, we can add the implementation in the cpp file for the player pawn.
void AcVRPlayerPawn::SetHandAnimationBlueprint(USkeletalMeshComponent* a_refHand) { static ConstructorHelpers::FObjectFinder HandAnimBP(TEXT("AnimBlueprintGeneratedClass'/Game/VirtualReality/Mannequin/Animations/RightHand_AnimBP.RightHand_AnimBP_C'")); if (HandAnimBP.Succeeded()) { a_refHand->AnimClass = HandAnimBP.Object; a_refHand->SetAnimationMode(EAnimationMode::AnimationBlueprint); a_refHand->SetAnimInstanceClass(HandAnimBP.Object); } else { UE_LOG(LogTemp, Error, TEXT("Could not load the hand anim BP")); } }
This function will need to be called at the end of the “CreateHandMesh” from the previous post. We pass in the reference to the hand skeletal mesh that we are creating in the function.
SetHandAnimationBlueprint(refComponentHand);
That is it. We have created the animation blueprint with the required variables and the states that will be needed for the grab action we will look into in the next series. We have loaded the animation blueprint and assigned this to the hand mesh. We just need to compile our code and the blueprint will be assigned to the hand mesh.
Thank you so much for making these tutorials!!! This is so helpful.