Download

How to attach a UCameraComponent to a capsule?

Hello all. I’m very new to Unreal, but decently familiar with C++, so I’ve been following the FPS tutorial over in the docs (found here, in particular the step I’m struggling with.)

My compiler is not very happy with the line


FPSCameraComponent->SetupAttachment(GetCapsuleComponent());

saying that it cannot convert a UCapsuleComponent* to a USceneComponent*.

I’m running Unreal 4.23, and I know that this tutorial is out of date, but it’s been working so far. Has Unreal changed the way this is handled? How can I attach a camera to a capsule component? Is there a way to convert it into a scene component?

I can’t really find anything online to help me, so any help would be appreciated! [HR][/HR]My current code, with the above line:
[SPOILER]FPSCharacter.h:



// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Camera/CameraComponent.h"
#include "FPSCharacter.generated.h"

UCLASS()
class FPSPROJECT_API AFPSCharacter : public ACharacter
{
    GENERATED_BODY()

public:
    // Sets default values for this character's properties
    AFPSCharacter();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public:
    // Called every frame
    virtual void Tick(float DeltaSeconds) override;

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

    // Handles input for moving forward and backward.
    UFUNCTION()
        void MoveForward(float Value);

    // Handles input for moving right and left.
    UFUNCTION()
        void MoveRight(float Value);

    // Sets jump flag when key is pressed.
    UFUNCTION()
        void StartJump();

    // Clears jump flag when key is released.
    UFUNCTION()
        void StopJump();

    // FPS camera.
    UPROPERTY(VisibleAnywhere)
        UCameraComponent* FPSCameraComponent;
};


FPSCharacter.cpp:



// Fill out your copyright notice in the Description page of Project Settings.

#include "FPSProject.h"
#include "FPSCharacter.h"

// Sets default values
AFPSCharacter::AFPSCharacter()
{
     // 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;

    // Create a first person camera component.
    FPSCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
    // Attach the camera component to our capsule component.
    FPSCameraComponent->SetupAttachment(GetCapsuleComponent());
    // Position the camera slightly above the eyes.
    FPSCameraComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f + BaseEyeHeight));
    // Allow the pawn to control camera rotation.
    FPSCameraComponent->bUsePawnControlRotation = true;
}

// Called when the game starts or when spawned
void AFPSCharacter::BeginPlay()
{
    Super::BeginPlay();

    if (GEngine)
    {
        GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("Using FPS Character."));
    }

}

// Called every frame
void AFPSCharacter::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
    PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
    PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);
    PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
    PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);
    PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);

}

//Input for moving forward/back.
void AFPSCharacter::MoveForward(float value)
{
    FVector Direction = AActor::GetActorForwardVector();
    AddMovementInput(Direction, value);
}

//Input for moving right/left.
void AFPSCharacter::MoveRight(float value)
{
    FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
    AddMovementInput(Direction, value);
}

void AFPSCharacter::StartJump()
{
    bPressedJump = true;
}

void AFPSCharacter::StopJump()
{
    bPressedJump = false;
}


[/SPOILER]

The issue is that it doesn’t know what a UCapsuleComponent is.

Add this to the CPP file:

#include “Components/CapsuleComponent.h”

That was it, thank you for the help!