I’m getting nowhere with this, I’ve been trying for days now but still nothing, how hard could it be to achieve something this simple?!
Anyway, here goes. I created a new blank Code project with no starter content. I then created a Pawn class and then added a mesh component and a camera component to it. That’s all really, simple. The mesh is the inside of the “cockpit”/cabin and the player is supposed to only be able too look around with the mouse, no other player input. The player should be moving along a predefined path, and I got that working, but I can’t look around. So I tried setting the camera to use the pawn’s control rotation, and then it worked! Now I can look around! Yeay! But… when the “cart” (or vehicle) start turning, the camera does not follow, it keeps looking in the same direction, making it look like the player is turning his head in the opposite direction then the cart is turning. And I have tried with all the settings I can find, for inheriting rotation from the parent and all that, but nothing works. So I tried getting the axis values from the axis mapping, but that only make the game crash.
Let’s look at some code:
:: HEADER ::
#pragma once
#include "GameFramework/Pawn.h"
#include "EgoCarPawn.generated.h"
UCLASS()
class BLANKPROJECT_API AEgoCarPawn : public APawn
{
GENERATED_BODY()
public:
AEgoCarPawn();
UPROPERTY(VisibleAnywhere)
class USceneComponent* CarPivot;
UPROPERTY(VisibleAnywhere)
class UStaticMeshComponent* CarMeshComponent;
UPROPERTY(VisibleAnywhere)
class UCameraComponent* DriverCamera;
virtual void BeginPlay() override;
virtual void Tick( float DeltaSeconds ) override;
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
};
:: SOURCE ::
#include "BlankProject.h"
#include "EgoCarPawn.h"
AEgoCarPawn::AEgoCarPawn()
{
PrimaryActorTick.bCanEverTick = true;
// Creates the base pivot for the car, this is what gets position and rotation updated in the Tick-function
CarPivot = CreateDefaultSubobject<USceneComponent>(TEXT("CarPivot"));
RootComponent = CarPivot;
// attach a mesh and adjust its postion and rotation to look correct
CarMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticComponent"));
static ConstructorHelpers::FObjectFinder<UStaticMesh>CarMesh(TEXT("StaticMesh'/Game/Models/car.car'"));
CarMeshComponent->SetStaticMesh(CarMesh.Object);
CarMeshComponent->AttachTo(CarPivot);
CarMeshComponent->SetRelativeRotation(FRotator(0.f, 180.f, 90.f));
CarMeshComponent->SetRelativeLocation(FVector(450.f, 0.f, 0.f));
// Attach and setup the camera
DriverCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("Driver Camera"));
DriverCamera->AttachTo(CarMeshComponent);
DriverCamera->bUsePawnControlRotation = false;
DriverCamera->SetRelativeLocation(FVector(310.f, -145.f, 39.f));
DriverCamera->SetRelativeRotation(FRotator(180.f, 0.f, -90.f));
DriverCamera->FieldOfView = 90.f;
}
void AEgoCarPawn::BeginPlay()
{
Super::BeginPlay();
}
void AEgoCarPawn::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
// ****
// code for getting the cars position and rotation goes here ..... cant show you
// ****
// Use the values that came from the hidden code above, to update the position and rotation of the car in the world, all this works fine
CarPivot->SetWorldLocation(FVector(x, y, z));
CarPivot->SetRelativeRotation(FRotator(pitch, yaw, roll));
// This is suppose to make the camera rotate when the player moves the mouse, but instead it crashes the game
FRotator HeadRotation = DriverCamera->RelativeRotation;
HeadRotation.Pitch += InputComponent->GetAxisValue(TEXT("LookUp"));
HeadRotation.Yaw += InputComponent->GetAxisValue(TEXT("LookRight"));
DriverCamera->RelativeRotation = HeadRotation;
}
void AEgoCarPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
check(InputComponent);
InputComponent->BindAxis(TEXT("LookUp"));
InputComponent->BindAxis(TEXT("LookRight"));
}
Why is the game crashing and how do I get the axis value from the mapped axis so I can modify the cameras rotation? It sounds like such a simple and basic thing to do in a game engine, but I can wrap my head around how Unreal works here.
Thanks for your time!