Unreal Engine10: Implementation of Character

written in front

Here is mainly to introduce the implementation of Character, and also introduce the acquisition of UE4 resources by the way;

1. UE4 resource acquisition

1. Obtaining the map

1.1 Download resources

  • Search for content in the Unreal Mall of Epic Games Launcher , and the ones with the environment tag are mainly map resources;
  • There are some free resources and resources suitable for beginners, and there will be free resources of this month every month, so it is still very good o(  ̄▽ ̄ );
  • When selecting resources, pay attention to the engine version supported by the resource and the engine version used;
  • Here the Sun Temple project is used as an example (since it is free and suitable for UE4.24);
  • After purchasing the item, switch the interface to Library -> Vault , where the item just purchased is placed;
  • Choose to create a project , and the project will be automatically downloaded to the specified local location;

create project

  • After the creation is complete, double-click the project in My Project to open it;

1.2 Loading into your own project

  • Then migrate the maps needed in the Sun Temple project to your own project;
  • Right-click on the Maps folder of the content browser -> Migrate , and the migration path is the Content folder under your own project ;
  • Map resources are usually placed in the Maps folder in the Content folder , so remember to conform to the specification when naming (refer to the previous Unreal Engine05: UE4 Basic Concepts article), so that there will be no confusion about the placement of resources after migration ;
  • If there is a Maps folder, it will be merged with the previous map resources and placed in the same folder. If not, you can just get a Maps folder;

1.3 Modify the default map

  • In the main panel of the Editor -> Settings -> Maps and Modes , modify the editor start map and the game default map ;

Modify the game default map

2. Acquisition of roles

1. Download resources

  • Search for content in the Epic Games Launcher's Unreal Mall , and the ones with the character tag are mainly character resources;
  • The finer the character modeling, the greater the character resources;
  • The animation starter content pack used here is free and small;

2. Load into your own project

  • Since the character resource is not a complete project, it is added to the project instead of creating the project ;

add to project

  • Just choose your own project to add. After adding, an AnimStarterPack folder will be added to the Content folder of the project, which contains the resources just added;

resources added

  • It contains resources such as character animation, skeleton, material and texture;

3. Bind to Character

  • Create a blueprint class with Character's C++ class;

Create a blueprint class

  • Click on Meshthe component (this component is inherited from ACharacter, and ordinary Pawn does not have it by default), and then select a character's skeleton in Mesh -> Skeletal Mesh ;

add bones

  • Then some adjustments are needed:
    • Move the armature to the center of the capsule body ;
    • Resize the capsule body CapsuleComponentto cover the skeleton ;
  • Adjusted as follows:

Adjusted skeleton

2. Create a Character C++ class

  • The created C++ class is also placed in the Public and Private folders in the Source folder;
  • Select Character as the inherited parent class;

head File

  • From the perspective of the header file only, the header files of the Character class and the Pawn class are the same, and there is no difference;
  • But from the perspective of the inherited ACharacterclass, you can see that there APawnare three more components than the class, as follows:

ACharacter class

1. Add camera and spring arm

  • Add camera and spring arm components to the header file;

head File

  • Initialize the spring arm and camera in the constructor;
    cpp implementation

  • Some points to note are as follows:

    • If the component bUsePawnControlRotationis set to true, the component will rotate with the Pawn's controller, that is, the rotation control can be added to the Pawn's controller , and there is no need to set the rotation of the spring arm separately;
    • The spring arm needs to follow the rotation, since the camera is mounted on the slot of the spring arm, it will automatically rotate with the spring arm, no need to set it separately;
  • After generating the blueprint class, be sure to use the Camera Settings -> Use Pawn to control the rotation in the details panel to confirm whether the spring arm and the camera use the controller settings are consistent with the code (because I found that even if it is written in C++ Checked , but still inconsistent in blueprint classes);

insert image description here

2. Add input control mapping

  • Add Jump, Look_Up and Turn_Right in Project Settings -> Input , as follows:

input mapping

  • Look_Up is actually the angle of view moving up and down, but here it is not directly controlling the camera or the spring arm, but passing it to the Controller;
  • Turn_Right is actually the angle of view moving left and right, but here it is not directly controlling the camera or the spring arm, but passing it to the Controller;

3. Implement input control with Controller

  • Since Pawn can be controlled by the Controller, it is best to implement the control function of its motion to the Controller instead of directly using the state of the Actor to realize the motion as before;
  • Define the response event function in the header file, as follows:

head File

  • Implementation MoveForward()and MoveRight()functions, as follows:

function implementation

  • Some points to note are as follows:

    • Here , the rotation angle component of the controller is used as the forward direction of the Pawn, instead of directly using GetActorRightVector()the function as before, that is, the control of the motion is placed on the Controller instead of directly on the Actor;
    • In this case, because the rotation angle of the controller is the rotation angle of the spring arm and the rotation angle of the camera, the current local coordinate system of the camera is actually used as the relative direction of the Pawn's movement;
    • Of course, this way of writing can only be used on Pawn and its derived classes , because Pawn can have Controller;
    • The recommendation is this way of writing , it does not conflict with the custom motion component (see: Unreal Engine09: Custom Pawn motion component article), because the function of the motion component is AddMovementInput()effective in the function, that is to say, as long as the function is used AddMovementInput(), it will automatically Define the motion component to take effect;
  • Implementation TurnAtRight()and TurnLookup()functions, as follows:

function implementation

  • Some points to note are as follows:
    • Here, the rotation value of the Controller is directly modified , instead of modifying the rotation value of the spring arm by SetActorRotation()modifying the rotation value of the Actor and calling the function of the spring arm component respectively as before;
    • That is, the rotation control is also added to the Controller, and then the spring arm rotates with the Controller ;
    • It is also recommended to write in this way, which is equivalent to handing over all control to the Controller;
    • pay attention:
      • The rate is used here to control the speed of the rotation, that is, the increased rotation angleAddControllerPitchInput() in is equal to the rotation rate multiplied by the time between two frames , which is equivalent to the logic previously implemented in , but there are no parameters passed in here;Tick()DeltaTime
      • TurnLookup()In the function, AddControllerPitchInput()the parameter passed in is -Valuebecause the Y value is positive when the mouse is pushed up , but in fact the controller (that is, the spring arm, because the spring arm rotates at the same angle as the controller) should rotate the viewing angle downward to be upward Look , so it is exactly the opposite of the incoming value;

4. Bind the input to the response event function

  • Bind the mapping in SetupPlayerInputComponent, as follows:

function implementation

  • Some points to note are as follows:
    • This is the first time that the operation mapping has been implemented, and two response event functions need to be bound , one is executed when the button is pressed, and the other is executed when the button is lifted;
    • The jumping action is directly called ACharacterthe built-in function, because this action input will neither cause the rotation of the viewing angle nor the movement of the Character position, so there may not be so many bells and whistles in the implementation;

5. Some other motion control settings

  • In the constructor, some more variables need to be set, as follows:
    function implementation

3. Animation Blueprint

  • The animation blueprint is used to allow Character to use animation during movement;

1. Create a new animation instance C++ class

  • The created C++ class is also placed in the Public and Private folders in the Source folder;
  • Select AnimInstance as the inherited parent class;

new class

  • At this time, the header file .cppis empty and has no other content, as follows:

head File

  • This animation instance class is mainly used to obtain some current states of Pawn , so that different animations can be performed according to the state of Pawn in subsequent animation control;

1. Header file

  • The implementation of the header file is as follows:

head File

  • Add two vectors to record the Pawn state in the header file, one is the current speed of the Pawn , and the other is whether the Pawn jumps into the air ;
  • PawnThe pointer points to the Pawn class object of the current animation, which means that the animation cannot be implemented without the character (this is logically reasonable);
  • NativeInitializeAnimation()It is the animation initialization function, which is equivalent to the function of the constructor;
  • UpdateAnimationProperties()It is used to obtain the motion state of Pawn and bind it to the current class attribute;

2. cpp implementation

  • Implement the initialization function as follows:

function implementation

  • The implemented UpdateAnimationProperties()function is as follows:

function implementation

2. Create a new animation blueprint class

  • Here, instead of directly deriving a blueprint class from a C++ class, a more advanced blueprint class needs to be created first;
  • Create a new Animationsfolder;
  • Right-click Animation -> Animation Blueprint in the content browser to create an animation blueprint:

Create animation blueprints

  • Select AnimInstanceas the parent class , and select the skeleton resource corresponding to Character for the target skeleton ;

Create animation blueprints

  • In this way, the animation can be bound to the bone resource;
  • Open the animation blueprint, select Class Settings -> Class Options -> Parent Class , and set it to the animation instance class you just created UManAnimInstance;

2.1 Create a new one-dimensional mixed space animation

  • In the content browser, right-click Animation -> Blend Space 1D to create a new one-dimensional blend space animation;
    1D blend space animation

  • The result of editing the one-dimensional mixed space animation in the animation interface is as follows:

Edit 1D blend animation

  • Some points to note are as follows:
    • At the bottom right of the interface is the animation resource browser , and the animations related to the current skeleton are already in it;
      • The green icon represents the animation sequence , which can be regarded as the most basic animation resource;
      • The orange icon represents the mixed space , which can be regarded as an animation resource formed by the superposition and combination of multiple animation sequences;
      • Double-click the animation resource to preview and modify the corresponding animation in the interface;
    • At the bottom of the interface is the one-dimensional animation axis (this is also the origin of the name of the one-dimensional mixing space), and existing animations can be placed at different positions on the axis as key frames to generate combined animations;
      • The green diamond point is the animation effect displayed in the current preview window;
      • The white diamond point is the key frame of the animation, usually at least one key frame is required at the beginning and the end;
    • Some settings of the one-dimensional animation axis can be modified in the resource details on the left side of the interface ;
      • Modify the name to name the one-dimensional animation axis;
      • Modifying the minimum axis value and maximum axis value can modify the definition domain of the axis;
      • Increasing the number of grid divisions can increase the position on the axis where keyframes can be inserted;
      • Modifying the interpolation type can adjust the transition animation generation effect between key frames;
    • Here, 0.0the Idle_Rifle_Hit animation resource is used to represent standing, 20.0the Walk_Fwd_Rifle_Ironsights animation resource is used to represent walking, and 100.0the Jog_Fwd_Rifle is used to represent jogging;
    • In this way, the production of one-dimensional mixed space animation is completed;

2.2 Create a new two-dimensional mixed space animation

  • In the content browser, right-click Animation -> Blend Space to create a new one-dimensional blend space animation;

Create a new 2D blend space

  • The result of editing the 2D blend space animation in the animation interface is as follows:

Edit 2D blend space animation

  • Some points to note are as follows:
    • Below the interface is the two-dimensional animation axis (this is also the origin of the name of the two-dimensional mixing space), and existing animations can be placed at different positions on the axis as key frames to generate combined animations;
      • The green diamond point is the animation effect displayed in the current preview window;
      • The white diamond point is the animation key frame, usually at least one key frame is required on each outermost edge;
    • Some settings of the one-dimensional animation axis can be modified in the resource details on the left side of the interface ;
      • Modify the name to name the one-dimensional animation axis;
      • Modifying the minimum axis value and maximum axis value can modify the definition domain of the axis;
      • Increasing the number of grid divisions can increase the position on the axis where keyframes can be inserted;
      • Modifying the interpolation type can adjust the transition animation generation effect between key frames;
    • Here the horizontal coordinate is named Direction, the range is [-180, 180], the vertical coordinate is named Speed, and the range is [0, 100];
    • The key frame still uses the three animations of standing, walking, and running used in the previous one-dimensional animation axis, but walking and running use the default four animations of forward, left, right, and backward-90 , and use left , 90using right, -180and 180backward, and 0forward, the specific animation sequence is as follows:

animation sequence

  • In this way, the production of two-dimensional mixed space animation is completed;

2.3 Writing blueprints

  • Write blueprint logic in the event graph window in the animation blueprint ;
  • UpdateAnimationProperties()Here you only need to call the function implemented in C++ every frame to get the status of Pawn ;
    blueprint

2.4 Writing Animated Charts

  • Add a state machine , named "Man State Machine", as follows:

state machine

  • Write a state machine, each state can be regarded as an animation, and different states have corresponding jump logic, as follows:

State machine implementation

  • The blueprint implementation of each state and jump logic is introduced below;
  • (1) Stand_Walk_Run state:
    • Through the control defined by C++, MovementSpeedthe three animations of standing -> walking -> running are realized, using the one-dimensional mixed space animation just created;
    • The input of the one-dimensional mixed space animation here is the abscissaSpeed of the one-dimensional animation axis just in the animation ;

state realization

  • (2) Go to the logic of Jump:
  • Use the C++ definition IsJumpingto control the jump, and the final output here is a Boolean result;

logic implementation

  • (3) Jump status:
  • Just use the existing animation sequence directly;

state realization

  • (4) Jump to the logic of In_Air:
    • A function of the remaining , that is, when the animation of the previous state is played to 20%, it will go to the next state;
    • But in fact, the vacated state is completely unnecessary. This is just to demonstrate the control of animation by the complex state machine;

state realization

  • (5) Status of In_Air:
  • Here, forward walking is used as the vacating animation, because there is no vacating animation in the animation resource;

state realization

  • (6) Go to the logic of On_Floor:
  • IsJumpingIf it is false, it will fall to the ground;

state realization

  • (7) Status of On_Floor:
  • Here only the animation after 0.676s of the Jump_From_Stand animation sequence is used, which can be set in Settings -> Initial Position in the details panel , and the loop animation should be unchecked ;

state realization

  • (8) Go to the logic of Stand_Walk_Run:
  • After the animation of the previous state is finished, it can jump to the initial state;

logic implementation

  • In addition, you can debug the Character's motion animation and state machine correspondingly, click Play on the current panel , and then select the corresponding object of the Character as follows;

debug state machine

3. Use the animation blueprint in Character

  • Select the Mesh component in the Character blueprint class , choose to use the animation blueprint in the Animation -> animation mode of the details panel , and select the animation blueprint class just created in the animation class ;

Guess you like

Origin blog.csdn.net/weixin_43992162/article/details/129098077