Vehicle Enhanced Input Binding with C++

I’m trying to implement a vehicle with enhanced input with C++. I could do make move a vehicle actor with blueprint by referencing the vehicle template. But I’m stuck this same thing with C++ now.

image

This above is my Mapping Context setting.

And this above is the enhanced input action to throttle the vehicle.

And I changed the Mapping Context as my own things at VehiclePlayerController, which is provided by vehicle game template.

VehiclePlayer.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "WheeledVehiclePawn.h"
#include "InputAction.h"
#include "InputMappingContext.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "ChaosVehicleMovementComponent.h"
#include "EnhancedInputComponent.h"

#include "VehiclePlayer.generated.h"

/**
 * 
 */
UCLASS()
class KARTRIDER_API AVehiclePlayer : public AWheeledVehiclePawn
{
	GENERATED_BODY()
	
public:
	AVehiclePlayer();

	void BeginPlay();

	void Tick(float DeltaTime);

	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	// Input Actions
	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Input Actions")
	UInputAction* ThrottleAction;

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category ="Input Actions")
	UInputAction* SteeringAction;

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Input Actions")
	UInputAction* BreakAction;

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Input Actions")
	UInputAction* DriftAction;

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Input Actions")
	UInputAction* ResetAction;


	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Input Mappings")
	UInputMappingContext* BaseMappingContext;

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Input Mappings")
	int32 BaseMappingPrioirty = 0;

	UPROPERTY(EditAnywhere, Category="Input")
	TSoftObjectPtr<UInputMappingContext> InputMapping;

private:
	UPROPERTY(EditAnywhere, Category="Vehicle Player")
	USkeletalMeshComponent* VehicleMeshComponent;

	UPROPERTY(EditAnywhere, Category = "Vehicle Player")
	USpringArmComponent* VehicleSpringArmComponent;

	UPROPERTY(EditAnywhere, Category = "Vehicle Player")
	UCameraComponent* VehicleCameraComponent;

	// Input Functions
	void EnhancedThrottle(const FInputActionValue& Value);

	void EnhancedSteering(const FInputActionValue& Value);

	void EnhancedBreak(const FInputActionValue& Value);

	void EnhancedDrift(const FInputActionValue& Value);

	void EnhancedReset(const FInputActionValue& Value);
};

VehiclePlayer.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "VehiclePlayer.h"


AVehiclePlayer::AVehiclePlayer()
{
	PrimaryActorTick.bCanEverTick = true;


	VehicleMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("VehicleMeshComponent"));
	RootComponent = VehicleMeshComponent;
	

	VehicleSpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("VehicleSpringArmComponent"));
	VehicleSpringArmComponent->SetupAttachment(VehicleMeshComponent);


	VehicleCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("VehicleCameraComponent"));
	VehicleCameraComponent->SetupAttachment(VehicleSpringArmComponent);

	// GetVehicleMovementComponent()->SetupVehicleMass()
}

void AVehiclePlayer::BeginPlay() {
	Super::BeginPlay();
}

void AVehiclePlayer::Tick(float DeltaTime) {
	Super::Tick(DeltaTime);

}

void AVehiclePlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	UEnhancedInputComponent* Input = Cast<UEnhancedInputComponent>(PlayerInputComponent);

	Input->BindAction(ThrottleAction, ETriggerEvent::Triggered, this, &AVehiclePlayer::EnhancedThrottle);
	Input->BindAction(ThrottleAction, ETriggerEvent::Completed, this, &AVehiclePlayer::EnhancedThrottle);


	Input->BindAction(SteeringAction, ETriggerEvent::Triggered, this, &AVehiclePlayer::EnhancedSteering);
	Input->BindAction(SteeringAction, ETriggerEvent::Completed, this, &AVehiclePlayer::EnhancedSteering);


	Input->BindAction(BreakAction, ETriggerEvent::Triggered, this, &AVehiclePlayer::EnhancedBreak);
	Input->BindAction(BreakAction, ETriggerEvent::Started, this, &AVehiclePlayer::EnhancedBreak);
	Input->BindAction(BreakAction, ETriggerEvent::Completed, this, &AVehiclePlayer::EnhancedBreak);


	Input->BindAction(DriftAction, ETriggerEvent::Triggered, this, &AVehiclePlayer::EnhancedDrift);
	Input->BindAction(DriftAction, ETriggerEvent::Started, this, &AVehiclePlayer::EnhancedBreak);
	Input->BindAction(DriftAction, ETriggerEvent::Completed, this, &AVehiclePlayer::EnhancedBreak);


	Input->BindAction(ResetAction, ETriggerEvent::Triggered, this, &AVehiclePlayer::EnhancedReset);
	

}

void AVehiclePlayer::EnhancedThrottle(const FInputActionValue& Value) {
	if (Value.GetMagnitude() != 0.0f)
	{
		// GetVehicleMovementComponent()->SetThrottleInput(Value.GetMagnitude());
		GetVehicleMovementComponent()->SetThrottleInput(Value.GetMagnitude());
	}
}

void AVehiclePlayer::EnhancedSteering(const FInputActionValue& Value) {
}

void AVehiclePlayer::EnhancedBreak(const FInputActionValue& Value) {
	GetVehicleMovementComponent()->SetBrakeInput(Value.GetMagnitude());
}

void AVehiclePlayer::EnhancedDrift(const FInputActionValue& Value) {

}

void AVehiclePlayer::EnhancedReset(const FInputActionValue& Value) {

}

Are the BindingAction in the SetupPlayerInputComponent wrong? or is the EnhancedThrottle function wrong? or both or something else? I’m trying to concentrate to solve only for Throttle now, and I think I can do for other actions same too, if I can solve for Thtrottle. Could anybody help…?

to make you vehicle move the problem is the Function which you call to perform the throttle action instead of getting the magnitude of your Value instead get its float value and make sure to set you input key in the editor to be Axis1D(float) and fix the code like

void AVehiclePlayer::EnhancedThrottle(const FInputActionValue& Value) {

    const float MoveDir=Value.Get<float>();
if (Controller &&(MoveDir!=0.0f)
{
	GetVehicleMovementComponent()->SetThrottleInput(MoveDir);
}

}