What You’ll learn
Below is a list of the topics this demo will address:
Player Inputs: Player control the movement of the character.
Character Controller: How to play simple animations and manipulate character bones.
Task Groups: How to handle tasks execution order.
3DVIA Studio User Interface :
You should be familiar with the key 3DVIA Studio interface elements, such as the views (Property view, Project view, editors) and basic drag and drop techniques.
Scripting Basics :
We assume that you already have a grasp of basic scripting principles.
Please visit our Video Tutorials section if you do not feel you meet these requirements yet.
Project Structure and Media
Download the Studio project HERE
The default stage contains the following environment objects :
an omni light
3 cubes which will be the look at targets
the application camera AppCamera
the character is defined by a vkSkeleton type in Studio which includes :
a mesh with attached material, texture and shader
a set of bones. The hierarchy can be expanded in the Project Editor
We want to move the character using the arrow keys and trigger a “fight” animation when pressing the “space” key.
We need first a behavior to manage user inputs.
1. Drag and drop from the Libraries->Presets->InputDevices the Input Manger behavior on the character (it should be highlighted in yellow).
Note: all the libraries behaviors are samples ready-to-use. But they have been created from scratch and should be customized to better fit your project.
2. From the project Editor, when we select the character we have now the InputsManager added to the skeleton. It is added to what we call the “component ring”.
We uncheck UseGamepad and Use Mouse. We can change the Up, left, right and down key. By default the action Key 1 is mapped on the “space” key.
A quick and easy way to trigger an animation when pressing a keyboard key consists in adding a KeyboardAnimation behavior on the character.
But in this tutorial we want to split the user inputs from the character behavior and understand how the behavior communicates.
3. Drag and drop from the Libraries->Presets->Characters the CharacterController behavior on the character .
4. From the project Editor, the character has another behavior on its component ring.
The behavior members list the animations and speed of rotation.
Set the Idle, Move Animation 1 and Gesture Animation 1. The behavior has 3 move animations but we’ll use only the walk animation. The exercise can be completed in playing a run or a crouch animation, depending of the key pressed.
Play the experience to check the Idle animation running.
How to get Inputs Manager data ?
In the Inputs Manager behavior we can see some members in the “Private” category. “Private” is not appropriate in our example because we want to access these values from outside the behavior by knowing the buttons state, expressed in boolean type.
In the task CheckInputs, at the line 62 we use the function IsKeyToggled() to set the state of the button1 member (ActionKey1 is the “space” bar in our example).
5. In the Character Controller Main task, we want to get the state of our ActionKey1 and play the “fight” animation if it is pressed.
To access to the InputsManager behavior members we need to declare it in the Target of the task. It will be automatically casted to the instance of the InputsManager attached to our character.
6. In the Execute loop of the task, we check the state of the button1 member. We use the function Gesture() with a EAnims parameter, listed in the SubTypes of the behavior.
Play the experience and press “space” key to trigger the fight animation.
7. We then need to add the walk and rotate animations. We use the functions Move(), RotateRight() and RotateLeft(). Let’s note that Move() code adds an animation to a “queue” to blend several animations. If the user doesn’t longer press the Up key we want the character to go back to the idle animation. That’s why we have an “else” statement for the Up key.
Play the experience and press Up, right, left keys. We note that the fight animation is naturally a secondary animation which is played smoothly during the walk.
Implement Look At
How to override an animation by moving some character bones ?
As seen in the project structure we can expand the bones hierarchy in the project editor. So we know which bone to move. In addition to the current animations the following code will rotate the bones as follow:
Head = target direction
Pelvis = character direction
Lower Torso = 1/4 of the angle between Pelvis and target direction
Torso = 2/4 of the angle between Pelvis and target direction
Neck = 3/4 of the angle between Pelvis and target direction
7. On the character controller behavior, create members to
get the bones and the target direction. The type will be vkNode3DPtr.
Create also usefull variable to perform the look at as the the target object and the speed to play the rotation. Also the transition will need some variables to play this transition : previous angle and current iteration.
finally set the values in the Project Editor. To select a bone, choose the “Pick…” option and select the bone in the hierarchy.
8. Create 3 usefull functions to perform the look at animation :
GetAngleBetweenPelvisAndObj() : get the angle between the pelvis and the target object. It is a simple dot product in the pelvis referential.
GetAngleBetweenPelvisAndHead() : get the angle between the pelvis and the current head orientation. Usefull to change direction from an existing head orientation. It is a simple dot product in the pelvis referential.
RotateTorsoAndHead() : with a unique angle parameter, rotate the 4 bones.
9. Create a task to execute the look at animation. We call it PostAnimation because we want to rotate the bones after the idle/move/gesture animations.
In the algorithm we first compute the rotation to perform, then perform the transition or keep the rotation.
The task should be executed at every frame because the idle/move/gesture reset all the bones orientation.
There is a test to limit the angle to 60°.
10. Because we want to execute this task after the standard animations we need to create a task group and position it in the PostProcess, after the Animation Manager.
In BeTaskGroup of the PostAnimation task we select our new task group.
11. Finally we write the picking of the target object, the init and the start of the task in Schematic.
Play the experience and click on a cube to look at it. The character can still walk and play an animation while we force the rotation of some bones.
You should have something very similar to this project at the end of this tutorial.