Hey there I’ve followed a tutorial (Link to unreal’s docs)
and I used a Character class instead of a Pawn, and I have some problems, mainly that my character’s forward is not actually the direction the camera is facing, and I don’t know how that could be fixed. Any ideas?
Code header:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "Protagonist_Character.generated.h"
UCLASS()
class PROJECT_BULWARK_API AProtagonist_Character : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AProtagonist_Character();
UPROPERTY(EditAnywhere)
UStaticMeshComponent* CharMesh;
UPROPERTY(EditAnywhere)
USpringArmComponent* CameraSpringArm;
UCameraComponent* ThirdPersonCamera;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
//MovementFunctions
void MovementForward(float amount);
void MovementRight(float amount);
//CameraFunctions
void CameraYaw(float value);
void CameraPitch(float value);
FVector2D MouseInputData;
FVector2D MoveInputData;
};
and the cpp:
#include "Protagonist_Character.h"
#include "Components/StaticMeshComponent.h"
#include "Components/InputComponent.h"
// Sets default values
AProtagonist_Character::AProtagonist_Character()
{
// 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;
CharMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CharacterMesh"));
CharMesh->SetupAttachment(RootComponent);
CameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
ThirdPersonCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
CameraSpringArm->SetupAttachment(RootComponent);
CameraSpringArm->TargetArmLength = 350.f;
CameraSpringArm->SetWorldRotation(FRotator(-60.f, 0.f, 0.f));
ThirdPersonCamera->SetupAttachment(CameraSpringArm, USpringArmComponent::SocketName);
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
// Called when the game starts or when spawned
void AProtagonist_Character::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AProtagonist_Character::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FRotator NewYaw = GetActorRotation();
NewYaw.Yaw -= MouseInputData.X;
FRotator NewPitch = CameraSpringArm->GetComponentRotation();
NewPitch.Pitch = FMath::Clamp(NewPitch.Pitch + MouseInputData.Y, -80.f, 0.f);
SetActorRotation(NewYaw);
CameraSpringArm->SetWorldRotation(NewPitch);
if (!MoveInputData.IsZero())
{
MoveInputData = MoveInputData.GetSafeNormal()* 500.f;
FVector NewLocation = GetActorLocation();
NewLocation += GetActorForwardVector()*MoveInputData.X*DeltaTime;
NewLocation += GetActorRightVector()*MoveInputData.Y*DeltaTime;
SetActorLocation(NewLocation);
}
}
// Called to bind functionality to input
void AProtagonist_Character::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &AProtagonist_Character::MovementForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AProtagonist_Character::MovementRight);
PlayerInputComponent->BindAxis("YawCamera", this, &AProtagonist_Character::CameraYaw);
PlayerInputComponent->BindAxis("PitchCamera", this, &AProtagonist_Character::CameraPitch);
}
void AProtagonist_Character::MovementForward(float amount)
{
MoveInputData.X = amount;
}
void AProtagonist_Character::MovementRight(float amount)
{
MoveInputData.Y = amount;
}
void AProtagonist_Character::CameraYaw(float value)
{
MouseInputData.X = value;
}
void AProtagonist_Character::CameraPitch(float value)
{
MouseInputData.Y = value;
}