Assembling a First-Person Setup With Controls

To make a main character, we will need a controller node implementing basic player functionality (handling keyboard and mouse input, movement settings, etc.). Attached to this node, we will have a first-person view camera, hands with a gun, and a body imitation to check for collisions with other characters, bullets, and environment. Later we will assign logic components to the nodes to implement shooting, visual effects, etc. Let's start with the character controller.

For the character controller, we will use the template first-person controller. It is included in the default scene of a new C++ project to simplify the programmer's task - find the Object Dummy named FirstPersonController in the hierarchy with the FirstPersonController component assigned.

Let's rename it to player. This node will represent our character.

Arranging a First-Person Setup#

For a first-person setup you will need the hands and weapon models, as well as animations previously created in a 3D modeling software. If you have your own assets, you're welcome to use them, otherwise you can use our ready-to-use assets available in the data folder.

We'll start with adding hands, and then attach a pistol to them. In the Asset Browser, find the data/fps/hands/hands.fbx asset and drag it into the viewport. Choose Skeleton Pose + Skinned Mesh in the popup menu.

To simulate a player's body that takes damage when hit by enemy bullets, let's create an additional object (it will approximate the player's body with a box):

  1. In the Menu bar, choose Create > Primitive > Box to create a box primitive of size (1, 1, 2), add it to the scene, and rename to player_hit_box.

  2. Add it as a child to the hands Dummy Node and reset its position to that of the parent. Go to the Parameters window, find the Surfaces section, and make sure the Intersection option for the box surface is enabled.

  3. Adjust the position of player_hit_box so that it is placed immediately below the hands.
  4. Make it invisible by clearing its Viewport mask in the Node tab of the Parameters window using the Clear All button. Then clear the Shadow mask to disable shadow rendering as well. The result should look like this:

We'll assign the Health component to it later.

Adding a Camera#

By default, FirstPersonController creates the camera during application execution, and to be able to see through the eyes of the character in UnigineEditor, you can create a new camera (PlayerDummy) and instruct the controller to use it. This will simplify testing of the first-person setup.

  1. Right-click the player Dummy Object, select Create > Camera > Dummy, and place the newly created camera somewhere in the scene.

  2. In the Node tab of the Parameters window, reset the position of the camera to the player Dummy Object position. Then adjust the rotation so that the camera is directed forward: set the rotation around the X axis to 90.

  3. Add the hands Dummy Node as a child to the PlayerDummy node.

  4. Adjust the position of the hands so that you can see them through the PlayerDummy camera (use the Current Camera drop-down to switch between PlayerDummy and Editor Camera). Transformation of the player's body should also be adjusted.

  5. In the Parameters window, you can change Near Clipping and FOV Degrees values of the PlayerDummy node to achieve the desired camera view.
  6. Check the Main Player box in the parameters of the PlayerDummy to make it the main camera.

  7. To avoid hands falling due to gravity, adjust the ShapeCapsule position of the player Dummy Object: switch to the Editor Camera view, select player Dummy Object in the World Nodes hierarchy, go to Parameters > Physics > Shapes and set the Z coordinate of the Position parameter to -0.8. The camera will be about 1.7 units above the bottom of the capsule, at eye level, which is lower than the door frames of the level. Then select the player_hit_box node and set the Z coordinate of its position to -0.8 as well, so that it almost coincides with the capsule.

  8. Select the player Dummy Object and go to the Node tab of the Parameters window.
  9. In the Node Properties section, choose Camera > Camera mode > USE EXTERNAL.
  10. Drag and drop the PlayerDummy node into the Camera field, then check the Use Object Body option in the Body group.

The PlayerDummy camera view should look like this:

Attaching a Weapon to the Hands#

In UNIGINE, you should use the Skinned Mesh with bones for animated models. A skinned mesh is displayed in the scene via an ObjectMeshSkinned node controlled by a Skeleton Pose node. Our FBX model of hands contains several bones. We can attach an object to a specific bone so that it follows its movement. For this purpose, we use a WorldTransformJoint node that has a controlled object (a pistol in our case) as a child, and the Skinned Mesh with bones as its parent.

  1. In the Asset Browser, find the data/fps/pistol/pistol.fbx asset and add it to the scene. Choose Skeleton Pose + Skinned Mesh in the popup menu.
  2. In the Menu bar, choose Create -> Mesh -> SkinnedBone. A WorldTransformJoint node will be created - place it somewhere in the scene. In the World Nodes hierarchy, add it as a child to the hands_mesh Skinned Mesh (the child of the hands Skeleton Pose node).

  3. In the Parameters window, find the Joint drop-down list, and select joint_hold. This will be the bone to which the pistol will be attached.

  4. Make the pistol a child of the WorldTransformJoint node. Reset its relative position and rotation to zero if needed.

Testing Animations#

There is also a number of animations to be used for the hands (idle, walking, shooting). You can check how a certain animation looks, for example:

  1. In the Asset Browser, select the data/fps/hands/hands_animations/hands_pistol_idle.anim file.

  2. In the Parameters window, find the Preview Mesh Skinned field and click Select Asset. Go to data/fps/hands/hands.fbx/hands.mesh_skinned, select the hands.mesh_skinned file, and click OK. You will see the animation preview in the Preview section.

Controlling Animations Using A Graph#

When the character changes its states (shooting, idle, walking forward/backward/left/right), the corresponding animations should change smoothly. To achieve seamless blending, we will use Animation Graph - a visual node-based system for creating and controlling character animations in UNIGINE. It allows building complex animation behaviors without writing low-level animation code. After that, we'll implement a component for interacting with the animations at runtime.

The following scheme shows the blend tree we are going to use:

The idle and movement animations (forward, left, right, backward) are integrated into a blend space, where they are synchronized and blended based on the movement vector. A boolean trigger controls the shooting transition.

For blending several animation states, we need to use a state machine. A state machine is the primary tool for structuring character animation behavior. It organizes animation logic into a set of states with transitions between them. Each state has its own nested graph which represents a single animation behavior (like locomotion or shooting). Inside states, you build the animation logic using nodes like Animation Player, Blend Poses, Blend Space, and others.

Adding Animation Graph#

To create a new animation graph:

  1. Right-click in the Asset Browser, choose Create > Animation Graph, and name it CharacterMotion. Double-click the created asset to open it in the Animation Graph Editor.

  2. Delete the default Animation Player node, right-click on the canvas, and type State Machine in the Search field. Rename the created node to main_sm and connect it to the Output Pose node.
  3. In the Mesh field of the Preview side window, click Open Asset and select the data/fps/hands/hands.fbx/hands.mesh_skinned file. Save the animation graph.

  4. Double-click the main_sm state machine to enter its nested graph and select the State_0 node. In the Selected Item side window, rename the node to locomotion - this state will implement idle and movement animations.

    Double-click the locomotion State node to open its internal graph.

  5. Delete the default Animation Player node, add the Blend Space 2D Sync node named locomotion_bs2d, and connect it to the Output Pose node.

    A blend space lets you mix multiple animations. Blend Space 2D Sync automatically calculates blend weights based on the current input position. It takes two Float inputs (X and Y) that define the current position in the blend space and output a single pose.

  6. In the Graph Parameters section, click and add two float variables named move_x and move_y. Set the variables' values as follows:

    • Minimum value: -1.0;
    • Maximum value: 1.0;
    • Default value: 0.0.

    Drag the variables into the canvas and connect them to the corresponding ports of the locomotion_bs2d node.

  7. Double-click the locomotion_bs2d node to enter the blend space editor. In the canvas that opens, select Point_0. Rename it to Idle in the Selected Item window and set its coordinates to [0, 0]. Choose the data/fps/hands/hands_animations/hands_pistol_idle.anim file in the Animation field, and set the animation Speed value to 30.

  8. Select Point_1 and rename it to Left. Change its coordinates to [-1, 0], choose the hands_pistol_walk_l.anim asset in the Animation field, and set the Speed value to 30 as well. Click Save.

    Now if you right-click and hold while moving the cursor between Idle and Left points, you will be able to preview the two animations blend.

  9. You can add new points in the blend space editor by right-clicking in the canvas area. Create three more points - Forward, Right, Backward - and set their parameters (coordinates, animation asset and speed) as follows:

    Your animation graph hierarchy should now look like this:

  10. Check Smooth Weights in the Blend Space 2D Sync Settings section to improve animation transitions - with this feature enabled, blend weights transition gradually over time instead of snapping instantly when the input position changes. You can also switch to Polar interpolation mode, which is better suited for diagonal movement.

Adding Logic#

Now when the animation graph is ready, let's add logic to control it at runtime.

  1. Add a new HandAnimationController component class in the IDE (Project → Add Class).

    Copy and paste the following code to the corresponding files:

    HandAnimationController.h

    Source code (C++)
    
    	#pragma once
    #include <UnigineComponentSystem.h>
    #include <UnigineGame.h>
    #include "FirstPersonController.h"
    
    class HandAnimationController :
        public Unigine::ComponentBase
    {
    public:
    	COMPONENT_DEFINE(HandAnimationController, Unigine::ComponentBase);
    
    	PROP_PARAM(Node, player_node, nullptr);
    
    	// declare methods to be called at the corresponding stages of the execution sequence
    	COMPONENT_INIT(init);
    	COMPONENT_UPDATE(update);
    
    	Unigine::Math::vec2 getLocalMovementVector();
    
    protected:
    	// world main loop overrides
    	void init();
    	void update();
    
    private:
    	FirstPersonController *fpsController = nullptr;
    
    	Unigine::NodeSkeletonPosePtr pose = nullptr;
    	Unigine::AnimScriptPtr animScript = nullptr;
    };
    							

    HandAnimationController.cpp

    Source code (C++)
    
    #include "HandAnimationController.h"
    
    REGISTER_COMPONENT(HandAnimationController);
    using namespace Unigine;
    using namespace Math;
    
    Unigine::Math::vec2 HandAnimationController::getLocalMovementVector()
    {
    		return Math::vec2(
    			Math::dot(fpsController->getSlopeAxisY(), fpsController->getHorizontalVelocity()),
    			Math::dot(fpsController->getSlopeAxisX(), fpsController->getHorizontalVelocity())
    		);
    
    }
    
    void HandAnimationController::init()
    {
    	fpsController = ComponentSystem::get()->getComponent<FirstPersonController>(player_node);
    
    	// get the node that this component is assigned to
    	// and cast it to NodeSkeletonPose type
    	pose = checked_ptr_cast<Unigine::NodeSkeletonPose>(node);
    
    	// get the animation control script
    	animScript = pose->getAnimScript();
    }
    
    void HandAnimationController::update()
    {
    	// update movement vector in animation parameters
    	vec2 movementVector = getLocalMovementVector();
    	animScript->setParamFloat("move_x", movementVector.y);
    	animScript->setParamFloat("move_y", movementVector.x);
    }
    							

    Build and run the application by hitting Ctrl + F5 in your IDE to make the Component System generate a property to assign the component to nodes. Close the application after running and return to UnigineEditor.

    Notice

    If you are building the Debug version of the application in the IDE, make sure to launch the Debug version of the Editor. The same applies to Release versions - both must match for the animation script to work correctly.

  2. Assign the property to the hands NodeSkeletonPose.
  3. Drag the player Dummy Object to the Player Node field of HandAnimationController so that it could get required data from the player's first person controller to perform blending.
  4. Assign CharacterMotion.agraph to the Animation Graph field in the Node Skeleton Pose section.

  5. Save all changes and run the application logic by hitting Run on the project's card in the SDK Browser to check the result.

The information on this page is valid for UNIGINE 2.22 SDK.

Last update: 2026-11-25 11:29:25