My camera is not behaving properly

I created a camera class with cameraboom, but inside the blueprint of the character, cameraboom doesn’t work.



Could you please advise how to properly implement a camera and bind it to a character with the ability to change CameraBoom parameters.

Show your code, please.

include “camera/CameraPawnBase.h”

ACameraPawnBase::ACameraPawnBase()
{
PrimaryActorTick.bCanEverTick = true;

CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
RootComponent = CameraBoom;
CameraBoom->TargetArmLength = 750.0f; // Distance from camera to pawn
CameraBoom->bUsePawnControlRotation = true; // Rotate arm based on controller
CameraBoom->bDoCollisionTest = false; // Disable collision test for SpringArm

FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom);

bUseControllerRotationPitch = false;
bUseControllerRotationRoll = false;
bUseControllerRotationYaw = false;

FieldOfView = 90.0f;
AspectRatio = 16.0f / 9.0f;
PostProcessBlendWeight = 0.0f;

}

void ACameraPawnBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}

void ACameraPawnBase::BeginPlay()
{
Super::BeginPlay();

APlayerController* PlayerController = Cast<APlayerController>(GetController());

if (PlayerController)
{
    this->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
}

}

void ACameraPawnBase::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

}

#pragma once

include “CoreMinimal.h”
include “GameFramework/Pawn.h”
include “Camera/CameraComponent.h”
include “GameFramework/SpringArmComponent.h”
include “CameraPawnBase.generated.h”

UCLASS()
class MYSTERYDARKEPIDEMIC_API ACameraPawnBase : public APawn
{
GENERATED_BODY()

public:

ACameraPawnBase();

protected:

virtual void BeginPlay() override;

public:

virtual void Tick(float DeltaTime) override;


virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

private:

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
USpringArmComponent* CameraBoom;

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
UCameraComponent* FollowCamera;


UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
float FieldOfView;

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
float AspectRatio;

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
float PostProcessBlendWeight;

};

Was your use of APawn instead of ACharacter intentional?
I think the problem is you’ve set the USpringArmComponent as your root object when it should be attached to the root (the APawn) instead.

Change this:

RootComponent = CameraBoom;

to this:

CameraBoom->SetupAttachment(RootComponent);

Thanks, I replaced that line of code you sent, but it didn’t help me.
I have a Character, but I am trying to implement the camera (camerapawn) separately from the Character. In the blueprint of the character I add a custom camera, but unfortunately it doesn’t work as if I wrote the camera inside the Character.

Could you show the code for the character as well?

#pragma once

include “CoreMinimal.h”
include “Charachter/DocCharacterBase.h”
include “camera/CameraPawnBase.h”
include “RipperCharacter.generated.h”

/**
*
*/
UCLASS()
class MYSTERYDARKEPIDEMIC_API ARipperCharacter : public ADocCharacterBase
{
GENERATED_BODY()
public:
ARipperCharacter();

};

// Copyright Anima Ludis Studio

include “Charachter/RipperCharacter.h”

include “GameFramework/CharacterMovementComponent.h”

ARipperCharacter::ARipperCharacter()
{
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->RotationRate = FRotator(0.f, 400.f, 0.f);
GetCharacterMovement()->bConstrainToPlane = true;
GetCharacterMovement()->bSnapToPlaneAtStart = true;

bUseControllerRotationPitch = false;
bUseControllerRotationRoll = false;
bUseControllerRotationYaw = false;

}

It’s an inherited character class.

This is the parent class

// Copyright Anima Ludis Studio

include “Charachter/DocCharacterBase.h”

// Sets default values
ADocCharacterBase::ADocCharacterBase()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = false;

Weapon = CreateDefaultSubobject<USkeletalMeshComponent>("Weapon");
Weapon->SetupAttachment(GetMesh(), FName("WeaponHandSocket"));
Weapon->SetCollisionEnabled(ECollisionEnabled::NoCollision);

}

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

}

You said you added a custom camera to the character. That’s the code I wanted to see.

  1. I was creating a blueprint from the clssass camera.
    2.I added the camera directly to the blueprint of the character, by dragging and dropping it into the hierarchy in the components.

If this is the wrong solution, then perhaps we should add attaching a custom camera inside the character code?
Maybe I should declare a variable to store the camera pointer!

Yeah, this is a strange setup. First of all, CameraCharacterBase is an actor, not a component, but here you are trying to manipulate the internals of the actor as if it was a component attached to RipperCharacter. The internals of CameraCharacterBase won’t be exposed in the RipperCharacter blueprint this way.

I think your latest post is correct and that you should just add a camera to the RipperCharacter instead and manipulate it from there.

1 Like

I will try a solution with implementing the camera inside the character code and let you know later if it helped.

// Copyright Anima Ludis Studio

#pragma once

include “CoreMinimal.h”
include “Charachter/DocCharacterBase.h”
include “camera/CameraPawnBase.h”
include “RipperCharacter.generated.h”

/**
*
*/
UCLASS()
class MYSTERYDARKEPIDEMIC_API ARipperCharacter : public ADocCharacterBase
{
GENERATED_BODY()
public:
ARipperCharacter();

protected:
virtual void BeginPlay() override;

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera")
ACameraPawnBase* CameraPawn;

};

// Copyright Anima Ludis Studio

include “Charachter/RipperCharacter.h”
include “camera/CameraPawnBase.h”
include “GameFramework/CharacterMovementComponent.h”
include “Player/DocPlayerController.h”

ARipperCharacter::ARipperCharacter()
{
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->RotationRate = FRotator(0.f, 400.f, 0.f);
GetCharacterMovement()->bConstrainToPlane = true;
GetCharacterMovement()->bSnapToPlaneAtStart = true;

bUseControllerRotationPitch = false;
bUseControllerRotationRoll = false;
bUseControllerRotationYaw = false;

}

void ARipperCharacter::BeginPlay()
{
Super::BeginPlay();

ADocPlayerController* PlayerController = Cast<ADocPlayerController>(GetController());

if (PlayerController)
{
	CameraPawn = GetWorld()->SpawnActor<ACameraPawnBase>();
	if (CameraPawn)
	{
		CameraPawn->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
	}
}

}

I tried adding a camera to the character’s code by declaring my camera component, but I never succeeded, the camera component is absent in the character’s BP

This is because your code is doing the same thing as adding the pawn by dragging and dropping it into the blueprint. You can’t expose the component of an actor attached to another actor in the parent blueprint.

Take the components out of the pawn that you want to use and attach them in ARipperCharacter.

1 Like