Announcement

Collapse
No announcement yet.

Vehicle input not working c++

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Vehicle input not working c++

    Hi everyone, I'm new to c++ and ue4 and I'm working on a basic race game with a simple cube as the vehicle. I've made a wheeled vehicle and I've attempted to add input from the player. My LookUp and Turn functions work perfectly fine but my ApplyThrottle and ApplySteering functions don't work at all. Also my cube doesn't drop down onto the track when I press play so that might be part of the problem. Here's my code.

    PlayerVehicle.h:
    #pragma once

    #include "CoreMinimal.h"
    #include "WheeledVehicle.h"
    #include "PlayerVehicle.generated.h"

    class USpringArmComponent;
    class UCameraComponent;

    UCLASS()
    class RACINGGAME_API APlayerVehicle : public AWheeledVehicle
    {
    GENERATED_BODY()

    public:

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "StaticMesh")
    UStaticMeshComponent* StaticMesh;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Camera")
    USpringArmComponent* SpringArm;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Camera")
    UCameraComponent* Camera;

    public:
    APlayerVehicle();

    // Called to bind functionality to input
    virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override;

    // Called every frame
    virtual void Tick(float DeltaTime) override;

    // Input functions
    void ApplyThrottle(float Value);
    void ApplySteering(float Value);

    void LookUp(float Value);
    void Turn(float Value);

    void OnHandbrakePressed();
    void OnHandbrakeReleased();
    };


    PlayerVehicle.cpp:
    #include "PlayerVehicle.h"

    #include "Components/StaticMeshComponent.h"
    #include "GameFramework/SpringArmComponent.h"
    #include "Camera/CameraComponent.h"
    #include "WheeledVehicleMovementComponent4W.h"
    #include "Components/InputComponent.h"

    #include "GameFramework/Controller.h"
    #include "Engine.h"

    static const FName NAME_SteerInput("Steer");
    static const FName NAME_ThrottleInput("Throttle");

    APlayerVehicle::APlayerVehicle()
    {
    UWheeledVehicleMovementComponent* Vehicle4W = CastChecked<UWheeledVehicleMovementComponent4W>(GetVehicleMovement());

    // Adjust tire loading
    Vehicle4W->MinNormalizedTireLoad = 0.0f;
    Vehicle4W->MinNormalizedTireLoadFiltered = 0.2f;
    Vehicle4W->MaxNormalizedTireLoad = 2.0f;
    Vehicle4W->MaxNormalizedTireLoadFiltered = 2.0f;

    // Torque setup which is the turning effect of a car
    Vehicle4W->MaxEngineRPM = 5700.0f;

    // Create a static mesh and set it as the root component
    StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));
    RootComponent = StaticMesh;

    // Create a spring arm component
    SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
    SpringArm->SetupAttachment(RootComponent);

    SpringArm->SetRelativeRotation(FRotator(-15.0f, 0.0f, 0.0f));
    SpringArm->TargetArmLength = 500.0f;
    SpringArm->TargetOffset = FVector(0.0f, 0.0f, 200.0f);

    SpringArm->bInheritPitch = true;
    SpringArm->bInheritYaw = true;
    SpringArm->bInheritRoll = false;

    // Lock the spring arm's rotation to follow controller input
    SpringArm->bUsePawnControlRotation = true;

    SpringArm->bAbsoluteRotation = false;

    // Create a following camera component
    Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
    Camera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);

    bUseControllerRotationPitch = true;
    bUseControllerRotationYaw = true;
    bUseControllerRotationRoll = false;

    // Auto possess player
    AutoPossessPlayer = EAutoReceiveInput::Player0;
    }

    void APlayerVehicle::Tick(float DeltaTime)
    {
    }

    void APlayerVehicle::SetupPlayerInputComponent(class UInputComponent* InputComponent)
    {
    Super::SetupPlayerInputComponent(InputComponent);

    InputComponent->BindAxis(NAME_ThrottleInput, this, &APlayerVehicle::ApplyThrottle);
    InputComponent->BindAxis(NAME_SteerInput, this, &APlayerVehicle::ApplySteering);
    InputComponent->BindAxis("LookUp", this, &APlayerVehicle::LookUp);
    InputComponent->BindAxis("Turn", this, &APlayerVehicle::Turn);

    InputComponent->BindAction("Handbrake", IE_Pressed, this, &APlayerVehicle::OnHandbrakePressed);
    InputComponent->BindAction("Handbrake", IE_Pressed, this, &APlayerVehicle::OnHandbrakeReleased);

    }

    void APlayerVehicle::ApplyThrottle(float Value)
    {
    GetVehicleMovementComponent()->SetThrottleInput(Value);
    }

    void APlayerVehicle::ApplySteering(float Value)
    {
    GetVehicleMovementComponent()->SetSteeringInput(Value);
    }

    void APlayerVehicle::LookUp(float Value)
    {
    if (Value != 0)
    AddControllerPitchInput(Value);
    }

    void APlayerVehicle::Turn(float Value)
    {
    if (Value != 0)
    AddControllerYawInput(Value);
    }

    void APlayerVehicle::OnHandbrakePressed()
    {
    GetVehicleMovementComponent()->SetHandbrakeInput(true);
    }

    void APlayerVehicle::OnHandbrakeReleased()
    {
    GetVehicleMovementComponent()->SetHandbrakeInput(false);
    }


    Any help is much appreciated, thanks
Working...
X