Creating & Implementing custom hand animations for VR in ue4. (Part 2)
Updated: Dec 3, 2020
(Part 2) Setting up custom blend animations so you can fire weapons.
If you haven't already I'd recommend you read/follow part one first, you can find it here: https://www.jonathanbardwell.co.uk/post/creating-and-implementing-custom-hand-animations-for-vr-in-ue4-part-1
You will also need to have the Pickup object to accept trigger inputs so we can change the animations. If your not sure how to do this I recommend following this blog post I wrote showing how to set it up.
If you have already followed both of the above tutorials, then welcome back. In this part, we're going to implement UE4's blend space animations so we can blend our hand animations to new ones while holding onto our objects. Think of this as your holding a gun but you also want to pull the trigger.
For this tutorial, I'll be carrying on with the same project that we created in Part One using UE4 4.25.3 this time.
If the animation you want to use already exists, don't make it again, you can reuse old animations if you need to but for this example, I'm going to create two more which we don't currently have being a trigger safety pose and a trigger pulled.
The First thing we need to do is create two new animations. We do this the exact same way we did it in Part 1. Duplicate the "Mannequin_Hand_Right_Open" animation provided in the VR template.
Selecting the bones I need to I'm going to re-position them into the necessary position. So my first animation looks like this.
Then we can go to "Create Asset" in the top left of our viewport, "Create Animation" and select "Current Pose". Make sure to save it to your animations folder so we keep or files organised.
Were now going to repeat the process and create our second animation which we are going to blend too, just repeat the above steps so you have your second animation. Mine looks like this and I've named it "Trigger Held"
You should now have two animations in your content browser we can use for our Blend space.
The next thing we need to do is create a Blend Space 1D. I'm going to do this in my animations folder so we keep everything we need in one place.
We can create our Blende Space 1D by right-clicking in the content browser - Animation - Selecting Blend Space 1D. it will then ask you to select a skeleton for us to use. In this case, we're using the right hand so we will need to select our "Mannequin_Hand_Right_Skeleton".
I named mine BS_TriggerPull (BS in replacement of Blend Space). While we are here I'm going to delete the "RightGrip_BS" as I'm not using it. (mine gave me a warning saying it was in use by the VR hands but it's not actually needed so I forced the delete).
Your content browser should now look something like this.
Now we can open our new Blend Space and start setting it up. This is pretty simple to do and only requires us to drag in out two animations we created. You can actually add more animations if you want them to blend between more versions. But in this case, we will stick to the two we created earlier.
You should now be able to see all your animations listed in the bottom right of the window and a graph at the bottom with a small green dot. This is where we are going to place our animations to let us blend between them.
To place our animations in the graph we just need to drag them in from the Asset Browser. The order and position does matter so we are going to place our first position animation (Trigger Safety) in the graph first. As far to the left as possible. And then place our Trigger Pull animation to the far right.
If you left-click and hold on the green dot you will see you can move it left and right in the graph. This is useful to see how our hand animations blend together.
[You can see if you add a third animation in the centre it will then blend between all three.]
With both animations added we can now close the BS_TriggerPull.
Were now going to open our RightHand_AnimBP which contains all our animations. and go to our AnimGraph and open our "Hand Pose State Machine" node
Now we are in the "Hand Pose" State machine were going to right-click and add a new state.
Were now going to name it "BlendSpaceToUse". This is going to be different to our other animations as were actually going to have our pickup object set it so we don't need to repeat it in the future.
Before connecting "BlendSpaceToUse" to the "HandPose" state were going to set some stuff up inside of it. Double click the state to go inside. Drag in our BS_Trigger from our Asset Browser and connect it to the "Output Animation Pose" node.
Select our newly created "Blendspace Player" node and go to the Details tab in the top right. by default X value is already exposed and all we need to do next is expose our blend space. You can do this by ticking the check box to the right of "Blend Space." in the details pannel.
We are then going to promote both inputs to variables so we can access them from our AC_SetHandAnimation actor component. Im going to name the Float "Trigger Pull Speed" "Value 0" and the "Blend Space Base" variable to "Blend Space To Use" and by default it should have our "BS_TriggerPull"
We now need to go back to our hand pose graph and set up our transition rules. but before we can we need to add a new name to our "GripEnum" or we won't be able to set our transition rules. Add a new Enum named "BlendSpace" then save it.
Back in our animation BP we can now set up our Transition rules. We do this the same way as the others. Drag from the "Hand Pose" state to our "Blend Space To Use" and back again.
in the transition rule going from Hand pose to blend we need to set up grip state == Blend space to result. it should look like the image bellow.
Our transition running in the opposite direction from "BlendSpaceToUse" to "Hand Pose" should be set to grip state NOT= Blend space to result. Like the image below.
We can now close our animation blueprint as we don't need it anymore. We can now open our AC_SetHAndAnimation actor component as were going to make some changes in it. The first thing we're going to do is clean up some code we created in part one which isn't actually needed.
This is our set Animation Bool in our "Set Hand Animation" Custom Event. We still need the variable but you can remove the set and delete it from the Event. We do need to make sure the "Set Animation" Bool in our variables panel is set to "True" though.
I recommend doing a quick test here of your project to make sure the hands still work as they should. Ignore my comment saying Fix this, I think there may be a better way to get the second controller but I'm not sure yet. If you know of one leave me a comment down below or head over to the Discord (link at the end).
The next thing we need to do is set our Blend Space to Use. We can do this by dragging of our Cast to RightHand_AnimBP and searching "Set BlendSpaceToUse" were going to connect this between the blueprint and our "SetAnimation" branch node.
Were now going to connect our Blend Space To Use to our Custom event so we can set it in our BP_PickupCube actor. Simple drag from our "Blend Space To Use" pin and let go over our "Set Hand Animation" event and it will create it for you.
We now need to create two custom events. I'm going to name one "Trigger Pull" and the Second "Trigger Release". This will allow us to set the trigger pull speed float we created in our animation Blueprint.
We now need to set our "Trigger Pull Speed" float values. We do this by dragging off our Cast To RightHand_AnimBP node. We will need two. One for our "Trigger Pull" event and one for our "Trigger Release"
The float connected to trigger Pull must be set to "100" and the Set node from Trigger Release must be set to "0"
Were now finished in AC_SetHandAnimation so we can now compile it and close it. We do need to head over to our BP_PickupCube.
When you open up the BP_PickupCube you may get a compile warning this is because we deleted the Bool from our AC_SetHandAnimation actor component. You can fix this by right-clicking the Red "Set animation" Text and selecting Reset to Default Value.
Were then going to promote the pin from Blend Space To USe to a function. and name it "BS To USe" hit compile and also connect it to the Set hand animation coming from the Event Drop. Your graph should look like the one below.
Make sure to set our new variable to Public (the little eye to the right of it).
Now we can set up the custom events we created in our AC_SetHandAnimation. Now we're going to use the two events we created in our last blog post.
If you haven't set up the BP_Interactable to accept trigger inputs from your Motion Controller you will need to do that now. If you don't know how or don't have it already set up I'd recommend following this blog post where I show you how. As I won't be covering it in this tutorial but you will need it. And coming back once you're done.
You should have two events one named "Event Interact" and a second "Called Event Stopinteract" were going to drag in our AC_SetHandAnimation into our graph between them so we can get a reference to it.
From the reference were going to drag off and get "Trigger Pull" and connect it to our "Event Interact" Node and were going to do the same for"Trigger Release" connecting it to our Event "Stop interact"
We're almost done now, we just need to tell our BP_PickupCube in our scene which animations to use and selecting our desired Blend Space.
Now we've told our BP_PickupCube which "Animation To Use" In this case our "BlendSpace" and set our "BS To Use" to our "BS_TriggerPull" we can test it out.
you'll see that the animations work exactly how they want to be the speed is two fast making it feel like it's snapping between both animations and not blending.
We can fix this by opening our "BS_TriggerPull" blend space we created and setting "Target Weight Interpolation Speed Per Sec" to a higher value. I personally think "15" is quite nice.
If we put on the headset and test again we will see it now blends between our animations correctly.
I hope this helps you with your project and id love to see how you've implemented it, Head on over to the GameDevXR Discord and share a video.
If this helped why not head over to my Patreon where I have a variety of VR content available for download as well as one to one tutoring if you need help.
If you enjoyed this or found it useful please consider Subscribing to my Youtube channel where I post regular VR Tutorials for Unreal Engine 4 and Blender.
or if you're looking for somewhere to hand out or need help with something VR, Game dev or 3D related maybe consider joining our growing discord community of over 300+ Vr developers.
Until next time, stay safe and I'll see you then.