I can’t for the life of me figure out why the camera is zooming in with W and S instead of moving forward and backward, especially when A and D are working correctly.
Can anyone see why that would be?
The Header File:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "InputConfigData.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "PlayerCam.generated.h"
UCLASS()
class THECRPGPROJECT_API APlayerCam : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
APlayerCam();
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UInputMappingContext* playerCamContext;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
class UInputConfigData* inputConfigData;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
void camMove(const FInputActionValue& Value);
void camLook(const FInputActionValue& Value);
void camRotate(const FInputActionValue& Value);
void camZoom(const FInputActionValue& Value);
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
USceneComponent* sceneComp;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
USpringArmComponent* springArmComp;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UCameraComponent* camComp;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float moveSpeed;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float rotSpeed;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float rotPitchMin;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float rotPitchMax;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float zoomSpeed;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float zoomMin;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float zoomMax;
private:
UPROPERTY()
FVector targetLoc;
UPROPERTY()
FRotator targetRot;
UPROPERTY()
float tarZoom;
UPROPERTY()
bool canRot;
};
The CPP FIle:
#include "PlayerCam.h"
#include "GameFramework/SpringArmComponent.h"
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "InputConfigData.h"
#include "Camera/CameraComponent.h"
// Sets default values
APlayerCam::APlayerCam()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
sceneComp = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComponent"));
RootComponent = sceneComp;
springArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
springArmComp->SetupAttachment(RootComponent);
springArmComp->TargetArmLength = 2000.0f;
springArmComp->bDoCollisionTest = false;
camComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
camComp->SetupAttachment(springArmComp);
moveSpeed = 20.0f;
rotSpeed = 2.0f;
rotPitchMin = 10.0f;
rotPitchMax = 80.0f;
zoomSpeed = 200.0f;
zoomMin = 500.0f;
zoomMax = 4000.0f;
}
// Called when the game starts or when spawned
void APlayerCam::BeginPlay()
{
Super::BeginPlay();
APlayerController* PlayerController = Cast<APlayerController>(Controller);
if (PlayerController)
{
UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer());
if (Subsystem)
{
Subsystem->AddMappingContext(playerCamContext, 0);
}
}
targetLoc = GetActorLocation();
tarZoom = 3000.0f;
const FRotator armIntRot = springArmComp->GetRelativeRotation();
targetRot = FRotator(armIntRot.Pitch - 50, armIntRot.Yaw, 0.0f);
}
// Called every frame
void APlayerCam::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
const FVector interpLoc = UKismetMathLibrary::VInterpTo(GetActorLocation(), targetLoc, DeltaTime, moveSpeed);
SetActorLocation(interpLoc);
const float interpZoom = UKismetMathLibrary::FInterpTo(springArmComp->TargetArmLength, tarZoom, DeltaTime, zoomSpeed);
springArmComp->TargetArmLength = interpZoom;
const FRotator interpRot = UKismetMathLibrary::RInterpTo(springArmComp->GetRelativeRotation(), targetRot, DeltaTime, rotSpeed);
springArmComp->SetRelativeRotation(interpRot);
}
void APlayerCam::camMove(const FInputActionValue& Value)
{
if (ensure(Value.GetValueType() == EInputActionValueType::Axis2D)) {
targetLoc += springArmComp->GetTargetRotation().RotateVector(Value.Get<FVector>()) * moveSpeed;
}
}
void APlayerCam::camLook(const FInputActionValue& Value)
{
if (ensure(Value.GetValueType() == EInputActionValueType::Axis1D)) {
const float newPitch = Value.Get<float>() * rotSpeed * 0.25;
targetRot = FRotator(targetRot.Pitch + newPitch, targetRot.Yaw, 0.f);
}
}
void APlayerCam::camRotate(const FInputActionValue& Value)
{
if (ensure(Value.GetValueType() == EInputActionValueType::Axis1D)) {
const float newRot = Value.Get<float>() * rotSpeed;
targetRot = FRotator(targetRot.Pitch, targetRot.Yaw + newRot, 0.f);
}
}
void APlayerCam::camZoom(const FInputActionValue& Value)
{
if (ensure(Value.GetValueType() == EInputActionValueType::Axis1D)) {
tarZoom = FMath::Clamp(tarZoom + (Value.Get<float>() * zoomSpeed), zoomMin, zoomMax);
}
}
// Called to bind functionality to input
void APlayerCam::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
{
EnhancedInputComponent->BindAction(inputConfigData->camMove, ETriggerEvent::Triggered, this, &APlayerCam::camMove);
EnhancedInputComponent->BindAction(inputConfigData->camLook, ETriggerEvent::Triggered, this, &APlayerCam::camLook);
EnhancedInputComponent->BindAction(inputConfigData->camRotate, ETriggerEvent::Triggered, this, &APlayerCam::camRotate);
EnhancedInputComponent->BindAction(inputConfigData->camZoom, ETriggerEvent::Triggered, this, &APlayerCam::camZoom);
}
}