I try to get my character to move. The log inside the move function is printed out, also it gets the character movement component. I don’t understand why the character isn’t moving. Also it has no gravity on it. What do I miss?
#include "MyCharacter.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
// Sets default values
AMyCharacter::AMyCharacter()
{
// 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;
// Setup component hierarchy
PlayerCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("PlayerCamera"));
PlayerCamera->SetupAttachment(GetCapsuleComponent());
PlayerMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("PlayerMesh"));
PlayerMesh->SetupAttachment(PlayerCamera);
}
void AMyCharacter::Restart()
{
if (APlayerController* PC = Cast<APlayerController>(GetController()))
{
// Get the Enhanced Input Local Player Subsystem from the Local Player related to our Player Controller.
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer()))
{
// PawnClientRestart can run more than once in an Actor's lifetime, so start by clearing out any leftover mappings.
Subsystem->ClearAllMappings();
// Add each mapping context, along with their priority values. Higher values out prioritize lower values.
Subsystem->AddMappingContext(BaseMappingContext, BaseMappingPriority);
}
}
}
void AMyCharacter::EnhancedMove(const FInputActionValue& Value)
{
const FVector2D MoveAxisVector = Value.Get<FVector2D>();
UE_LOG(LogTemp, Warning, TEXT("The MoveAxisVector value is: %s"), *MoveAxisVector.ToString());
const FVector Forward = GetActorForwardVector();
AddMovementInput(Forward, MoveAxisVector.Y * Speed);
const FVector Right = GetActorForwardVector();
AddMovementInput(Right, MoveAxisVector.X * Speed);
}
void AMyCharacter::EnhancedLook(const FInputActionValue& Value)
{
const FVector2D LookAxisVector = Value.Get<FVector2D>();
//UE_LOG(LogTemp, Warning, TEXT("The LookAxisVector value is: %s"), *LookAxisVector.ToString());
if (!LookAxisVector.IsNearlyZero()) {
AddControllerPitchInput(LookAxisVector.Y);
AddControllerYawInput(LookAxisVector.X);
}
}
// Called to bind functionality to input
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
check(PlayerInputComponent);
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
{
// Register action input bindings
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &AMyCharacter::EnhancedLook);
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AMyCharacter::EnhancedMove);
}
}