I have create an actor that handle input and play animation but I don’t seem to be able to make it work. I know there is something missing but I can’t seem to find it.
MyActorAnimation.h
#include "GameFramework/Actor.h"
#include "MyActorAnimation.generated.h"
UCLASS()
class MYCARPROJECT_API AMyActorAnimation : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActorAnimation();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
void PlayJump();
UInputComponent* InputComponent;
UPROPERTY(EditAnywhere) USkeletalMeshComponent* Mesh;
UPROPERTY(EditAnywhere) TArray<UAnimationAsset*> Anim;
};
MyActorAnimation.cpp
#include "MyCarProject.h"
#include "MyActorAnimation.h"
// Sets default values
AMyActorAnimation::AMyActorAnimation()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AMyActorAnimation::BeginPlay()
{
Super::BeginPlay();
InputComponent = ConstructObject<UInputComponent>(UInputComponent::StaticClass(), this, "Input Component");
InputComponent->BindAction("Jump", EInputEvent::IE_Pressed, this, &AMyActorAnimation::PlayJump);
}
// Called every frame
void AMyActorAnimation::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
void AMyActorAnimation::PlayJump() {
GEngine->AddOnScreenDebugMessage(-1, -1, FColor::Red, TEXT("Input1"));
Mesh->PlayAnimation(Anim[0], true);
}
static voidlol was correct in that you need to enable input for the actor. If you override the EnableInput function that static voidlol linked, you can bind your input as you did in line 20 of the .cpp. You can then call the Enable Input function on begin play to have that actor set to receive input when the game beings.