I’m trying to create a punch animation in a study project of mine, but it’s not running, I wrote the code and I think it’s correct, everything is assigned and when I add the logs they come out correctly, but the animation doesn’t run, which could it be?
Sorry for the words in portuguese, it’s because i’m form Brazil
// Fill out your copyright notice in the Description page of Project Settings.
#include "Personagem.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Animation/AnimMontage.h"
#include "Animation/AnimMontage.h"
#include "Kismet/KismetSystemLibrary.h"
// Sets default values
APersonagem::APersonagem()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
SpringArm->SetupAttachment(GetRootComponent());
SpringArm->TargetArmLength = 400.f;
Camera->SetupAttachment(SpringArm);
//VelAndando == SpeedWalking and VelCorrendo == SpeedRunning
VelAndando = 600.f;
VelCorrendo = 1200.f;
}
// Called when the game starts or when spawned
void APersonagem::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void APersonagem::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void APersonagem::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis(FName("FrenteTras"), this, &APersonagem::Moverfrentetras);
PlayerInputComponent->BindAxis(FName("MoverLados"), this, &APersonagem::Moverlados);
//MoverFrenteTras == MoveFrontandBackward and MoverLados == MoveSides
PlayerInputComponent->BindAxis(FName("MoverHorizontal"), this, &APersonagem::MoverHorizontal);
PlayerInputComponent->BindAxis(FName("MoverVertical"), this, &APersonagem::MoverVertical);
//MoverHorizontal == MoveHorizontally and MoverVertical == MoveVertically
PlayerInputComponent->BindAction(FName("Correr"),IE_Pressed, this, &APersonagem::Corrida);
PlayerInputComponent->BindAction(FName("Correr"), IE_Released, this, &APersonagem::PararCorrida);
//Correr == Run
PlayerInputComponent->BindAction(FName("Pular"), IE_Pressed, this, &APersonagem::Jump);
PlayerInputComponent->BindAction(FName("Pular"), IE_Released, this, &APersonagem::StopJumping);
//Pular == Jump
PlayerInputComponent->BindAction(FName("Soco"), IE_Released, this, &APersonagem::IA_Attack);
//Soco == Punch
}
void APersonagem::Moverfrentetras(float Value)
{
const FRotator ControlRotation = GetControlRotation();
const FRotator YawRotation(0.f, ControlRotation.Yaw, 0.f);
const FVector Direcao = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
//Direcao == Direction
AddMovementInput(Direcao, Value);
}
void APersonagem::Moverlados(float Value)
{
const FRotator ControlRotation = GetControlRotation();
const FRotator YawRotation(0.f, ControlRotation.Yaw, 0.f);
const FVector Direcao = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
//Direcao == Direction
AddMovementInput(Direcao, Value);
}
void APersonagem::MoverHorizontal(float Value)
{
AddControllerYawInput(Value);
}
void APersonagem::MoverVertical(float Value)
{
AddControllerPitchInput(Value);
}
void APersonagem::Corrida()
{
GetCharacterMovement()->MaxWalkSpeed = VelCorrendo;
//VelAndando == SpeedRunning
}
void APersonagem::PararCorrida()
{
GetCharacterMovement()->MaxWalkSpeed = VelAndando;
//VelAndando == SpeedWalking
}
void APersonagem::IA_Attack()
{
if (PunchAnimation)
{
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance)
{
AnimInstance->Montage_Play(PunchAnimation, 1.0f);
}
}
}
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Personagem.generated.h"
class UCameraComponent;
class USpringArmComponent;
class UAnimMontage;
UCLASS()
class LUTA_API APersonagem : public ACharacter
{
GENERATED_BODY()
public:
APersonagem();
virtual void Tick(float DeltaTime) override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
//MoveFrontandBack
void Moverfrentetras(float Value);
//MoveSide(Keyboard A and D)
void Moverlados(float Value);
//Move the mouse in horizontal
void MoverHorizontal(float Value);
//Move the mouse in vertical
void MoverVertical(float Value);
//void Running
void Corrida();
//void StopRunning
void PararCorrida();
void IA_Attack();
protected:
virtual void BeginPlay() override;
private:
UPROPERTY(EditAnywhere)
UCameraComponent* Camera;
UPROPERTY(EditAnywhere)
USpringArmComponent* SpringArm;
UPROPERTY(EditAnywhere)
UAnimMontage* PunchAnimation;
float VelAndando;
//float speedWalking
float VelCorrendo;
//float speedRunning
};