How do you bind the wasd keys in the top down c++ template? In the project settings, I can see that they are already setup, but in the PlayerController, when I try to do :
InputComponent->BindAxis("MoveForward", this, &AtopdowntestPlayerController::OnMoveForward);
It tells me
error: no matching member function for call to 'BindAxis'
even if the autocomplete suggests it to me.
PlayerController.cpp:
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#include "topdowntestPlayerController.h"
#include "AI/Navigation/NavigationSystem.h"
#include "Runtime/Engine/Classes/Components/DecalComponent.h"
#include "HeadMountedDisplayFunctionLibrary.h"
#include "topdowntestCharacter.h"
AtopdowntestPlayerController::AtopdowntestPlayerController()
{
bShowMouseCursor = true;
DefaultMouseCursor = EMouseCursor::Crosshairs;
}
void AtopdowntestPlayerController::PlayerTick(float DeltaTime)
{
Super::PlayerTick(DeltaTime);
// keep updating the destination every tick while desired
if (bMoveToMouseCursor)
{
MoveToMouseCursor();
}
}
void AtopdowntestPlayerController::SetupInputComponent()
{
// set up gameplay key bindings
Super::SetupInputComponent();
InputComponent->BindAction("SetDestination", IE_Pressed, this, &AtopdowntestPlayerController::OnSetDestinationPressed);
InputComponent->BindAction("SetDestination", IE_Released, this, &AtopdowntestPlayerController::OnSetDestinationReleased);
// support touch devices
InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &AtopdowntestPlayerController::MoveToTouchLocation);
InputComponent->BindTouch(EInputEvent::IE_Repeat, this, &AtopdowntestPlayerController::MoveToTouchLocation);
InputComponent->BindAction("ResetVR", IE_Pressed, this, &AtopdowntestPlayerController::OnResetVR);
// support keyboard controlls
InputComponent->BindAxis("MoveForward", this, &AtopdowntestPlayerController::OnMoveForward);
InputComponent->BindAxis("MoveRight", this, &AtopdowntestPlayerController::OnMoveRight);
}
void AtopdowntestPlayerController::OnResetVR()
{
UHeadMountedDisplayFunctionLibrary::ResetOrientationAndPosition();
}
void AtopdowntestPlayerController::MoveToMouseCursor()
{
if (UHeadMountedDisplayFunctionLibrary::IsHeadMountedDisplayEnabled())
{
if (AtopdowntestCharacter* MyPawn = Cast<AtopdowntestCharacter>(GetPawn()))
{
if (MyPawn->GetCursorToWorld())
{
UNavigationSystem::SimpleMoveToLocation(this, MyPawn->GetCursorToWorld()->GetComponentLocation());
}
}
}
else
{
// Trace to see what is under the mouse cursor
FHitResult Hit;
GetHitResultUnderCursor(ECC_Visibility, false, Hit);
if (Hit.bBlockingHit)
{
// We hit something, move there
SetNewMoveDestination(Hit.ImpactPoint);
}
}
}
void AtopdowntestPlayerController::MoveToTouchLocation(const ETouchIndex::Type FingerIndex, const FVector Location)
{
FVector2D ScreenSpaceLocation(Location);
// Trace to see what is under the touch location
FHitResult HitResult;
GetHitResultAtScreenPosition(ScreenSpaceLocation, CurrentClickTraceChannel, true, HitResult);
if (HitResult.bBlockingHit)
{
// We hit something, move there
SetNewMoveDestination(HitResult.ImpactPoint);
}
}
void AtopdowntestPlayerController::SetNewMoveDestination(const FVector DestLocation)
{
APawn* const MyPawn = GetPawn();
if (MyPawn)
{
UNavigationSystem* const NavSys = GetWorld()->GetNavigationSystem();
float const Distance = FVector::Dist(DestLocation, MyPawn->GetActorLocation());
// We need to issue move command only if far enough in order for walk animation to play correctly
if (NavSys && (Distance > 120.0f))
{
NavSys->SimpleMoveToLocation(this, DestLocation);
}
}
}
void AtopdowntestPlayerController::OnSetDestinationPressed()
{
// set flag to keep updating destination until released
bMoveToMouseCursor = true;
}
void AtopdowntestPlayerController::OnSetDestinationReleased()
{
// clear flag to indicate we should stop updating the destination
bMoveToMouseCursor = false;
}
void AtopdowntestPlayerController::OnMoveForward()
{
}
void AtopdowntestPlayerController::OnMoveRight()
{
}
PlayerController.h :
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "topdowntestPlayerController.generated.h"
UCLASS()
class AtopdowntestPlayerController : public APlayerController
{
GENERATED_BODY()
public:
AtopdowntestPlayerController();
protected:
/** True if the controlled character should navigate to the mouse cursor. */
uint32 bMoveToMouseCursor : 1;
// Begin PlayerController interface
virtual void PlayerTick(float DeltaTime) override;
virtual void SetupInputComponent() override;
// End PlayerController interface
/** Resets HMD orientation in VR. */
void OnResetVR();
/** Navigate player to the current mouse cursor location. */
void MoveToMouseCursor();
/** Navigate player to the current touch location. */
void MoveToTouchLocation(const ETouchIndex::Type FingerIndex, const FVector Location);
/** Navigate player to the given world location. */
void SetNewMoveDestination(const FVector DestLocation);
/** Input handlers for SetDestination action. */
void OnSetDestinationPressed();
void OnSetDestinationReleased();
/** Input handlers for keyboard axis controlls */
void OnMoveForward();
void OnMoveRight();
};