I can’t seem to be able to control my pawn. The camera doesn’t kick in, and instead of controlling the pawn, I control the default flying-player thing.
Here’s my code:
[MyCharacter.h]
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Pawn.h"
#include "MyCharacter.generated.h"
UCLASS()
class ACTUALPROJECT_API AMyCharacter : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMyCharacter();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
// Attaching the camera
virtual void AttachCamera(UCameraComponent*);
// Our axis bindings
virtual void MoveY(float);
virtual void MoveX(float);
UPROPERTY(EditAnywhere)
USceneComponent* VisibleComponent;
};
[MyCharacter.cpp]
// Fill out your copyright notice in the Description page of Project Settings.
#include "ActualProject.h"
#include "MyCharacter.h"
#include "Runtime/Engine/Classes/Components/InputComponent.h"
// Sets default values
AMyCharacter::AMyCharacter()
{
// 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;
// Set this pawn to be controlled by the lowest-numbered player
AutoPossessPlayer = EAutoReceiveInput::Player0;
// Create and attach our root component
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
// Create and attach our camera
UCameraComponent* Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("PlayerCamera"));
// Create the visible object
VisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisibleComponent"));
// Attach the camera
AMyCharacter::AttachCamera(Camera);
// Attach the visible component to the root
VisibleComponent->AttachTo(RootComponent);
}
// Attach the camera
void AMyCharacter::AttachCamera(UCameraComponent* camera)
{
camera->AttachTo(RootComponent);
camera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
camera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
}
// Called when the game starts or when spawned
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyCharacter::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
// Called to bind functionality to input
void AMyCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
// Binding axis to move
InputComponent->BindAxis("MoveY", this, &AMyCharacter::MoveY);
InputComponent->BindAxis("MoveX", this, &AMyCharacter::MoveX);
}
void AMyCharacter::MoveY(float vel)
{
if (vel == 0.0f)
return;
// The current actor location
const FVector CurrentLocation = GetActorLocation(RootComponent);
// Setting the actor location, with the velocity
SetActorLocation(FVector(CurrentLocation.X, CurrentLocation.Y + vel, CurrentLocation.Z));
}
void AMyCharacter::MoveX(float vel)
{
if (vel == 0.0f)
return;
// The current actor location
const FVector CurrentLocation = GetActorLocation(RootComponent);
// Setting the actor location, with the velocity
SetActorLocation(FVector(CurrentLocation.X + vel, CurrentLocation.Y, CurrentLocation.Z));
}