I’ve been following the book learning c++ by creating games on ue4. I’m now trying to implement player input, but cant get it to work. The book is kinda old, so maybe things have changed since then. I’ve looked for some help at the unreal input page and a few tutorials on youtube (the ones that aren’t on blueprints), but neither seem to work.
My header file:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Avatar.generated.h"
UCLASS()
class GOLDENEGG_API AAvatar : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AAvatar();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick(float DeltaSeconds) override;
public:
// New! These 3 new member function declarations
// they will be used to move our player around!
void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
void MoveForward(float amount);
void MoveRight(float amount);
};
My cpp file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Avatar.h"
// Sets default values
AAvatar::AAvatar()
{
// 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;
}
// Called when the game starts or when spawned
void AAvatar::BeginPlay()
{
Super::BeginPlay();
}
void AAvatar::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
check(InputComponent);
InputComponent->BindAxis("MoveForward", this, &AAvatar::MoveForward);
InputComponent->BindAxis("Strafe", this, &AAvatar::MoveRight);
}
void AAvatar::MoveForward(float amount)
{
if (Controller && amount)
{
FVector fwd = GetActorForwardVector();
AddMovementInput(fwd, amount);
}
}
void AAvatar::MoveRight(float amount)
{
if (Controller && amount)
{
FVector right = GetActorRightVector();
AddMovementInput(right, amount);
}
}