[UE4 C + +] realize character attack

Posted by prueba123a on Thu, 30 Dec 2021 02:10:27 +0100

Effect display

Using animated montage to realize character attack
After pressing the left key, the character will attack:



Random output attack animation.

Create animated Montage

Insert our animation into it:

Right click and click the new montage section:

Click Preview in the Sections section to divide it into multiple Sections:

Pull to the bottom:
Create a notification in notifications. The notification moves to the part corresponding to the end of the animation. The notification representative means that the action is over.


Click the magnifying glass to see the slot management. The second is our current animation, which can be renamed or added.

Add animation montage to animation blueprint

Open the animation blueprint and see the state machine part:

This is the top state. Add our montage part to the state.

Binding button and implementing trigger function

Bind button in project settings:

Left click attack.

To open a role C + + file:

. h file

  • Declare the animation montage and bind the animation montage to the task
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
	class UAnimMontage* _AnimMontage;

  • Whether the storage is in the attack state, and some other states will be restricted bool batacking;

  • Whether the storage is in the left mouse click state is used to judge whether continuous attacks can be carried out, bool bClicking;

  • Click start and end

UFUNCTION()
	void OnClickBegin();

UFUNCTION()
	void OnClickEnd();
  • Start and end of attack
void AttackBegin();

UFUNCTION(BlueprintCallable)
	void AttackEnd();

. cpp file

First, limit some things:

  • OnInteract(),MoveForward(float Value),MoveRight(float Value)
    Add: if (bAttacking) return;, You can't move when attacking. Pick up things.

  • The attack start function will open the attack state, obtain the animation instance, judge whether there is an animation montage, whether the instance exists, and whether the instance is playing the animation montage. If the conditions are met, start playing the animation montage. Next, let the animation jump to the part we need to play, and use the random function to output which action we want to output.

Add header file:

#include "Components/SkeletalMeshComponent.h"
#include "Animation/AnimInstance.h"
void ARole::AttackBegin()
{
	bAttacking = true;
	UAnimInstance* Instance = GetMesh()->GetAnimInstance();
	if (_AnimMontage && Instance && !Instance->Montage_IsPlaying(_AnimMontage)) {
		Instance->Montage_Play(_AnimMontage);

		switch (FMath::RandRange((int32)1, 3)) {
		case 1 : 
			Instance->Montage_JumpToSection(FName("Attack1"), _AnimMontage);
			break;
		case 2 : 
			Instance->Montage_JumpToSection(FName("Attack2"), _AnimMontage);
			break;
		case 3 : 
			Instance->Montage_JumpToSection(FName("Attack3"), _AnimMontage);
			break;
		}
	}
}
  • The attack is over
void ARole::AttackEnd()
{
	bAttacking = false;
	// If the mouse is still pressed, continue the attack
	if (bClicking && EquiqedWeapon) { 
		AttackBegin();
	}
}
  • Press the mouse to call the attack function
void ARole::OnClickBegin()
{
	bClicking = true;
	if (!bAttacking && EquiqedWeapon) {
		AttackBegin();
	}
}
  • Mouse release
void ARole::OnClickEnd()
{
	bClicking = false;
}
  • SetupPlayerInputComponent binding key

Press to call the press function, and release to call the release function.

// Attack action
PlayerInputComponent->BindAction("Click", IE_Pressed, this, &ARole::OnClickBegin);
PlayerInputComponent->BindAction("Click", IE_Released, this, &ARole::OnClickEnd);

Head and tail work

Associate our end notification with the end function in the animation blueprint.

The overall idea is: mouse down - OnClickBegin - AttackBegin - play animation - Animation end notification - AttackEnd -... (continuous attack) - mouse release - OnClickEnd - end.

Topics: C++ Game Development UE4 visualstudio