Characters/RPGHeroCharacter.h
添加如下代码
#pragma once
#include "CoreMinimal.h"
#include "Characters/RPGBaseCharacter.h"
#include "RPGHeroCharacter.generated.h"
class UCameraComponent;
class USpringArmComponent;
/**
*
*/
UCLASS()
class RPGFRAMEWORK_API ARPGHeroCharacter : public ARPGBaseCharacter
{
GENERATED_BODY()
public:
ARPGHeroCharacter();
public:
private:
#pragma region Components
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera", meta = (AllowPrivateAccess = "true"))
TObjectPtr<USpringArmComponent> CameraBoom;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera", meta = (AllowPrivateAccess = "true"))
TObjectPtr<UCameraComponent> FollowCamera;
#pragma endregion
};
Characters/RPGHeroCharacter.cpp
#include "Characters/RPGHeroCharacter.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
ARPGHeroCharacter::ARPGHeroCharacter()
{
GetCapsuleComponent()->InitCapsuleSize(42.0f, 96.0f);
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(GetRootComponent());
CameraBoom->TargetArmLength = 200.0f;
CameraBoom->SocketOffset = FVector(0.0f, 55.f, 65.f);
CameraBoom->bUsePawnControlRotation = true;
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
FollowCamera->bUsePawnControlRotation = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f);
GetCharacterMovement()->MaxWalkSpeed = 400.0f;
GetCharacterMovement()->BrakingDecelerationWalking = 2048.0f;
}
Characters/RPGBaseCharacter.h
// Copyright ChenGangQiang
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "RPGBaseCharacter.generated.h"
UCLASS()
class RPGFRAMEWORK_API ARPGBaseCharacter : public ACharacter
{
GENERATED_BODY()
public:
ARPGBaseCharacter();
};
.cpp
// Copyright ChenGangQiang
#include "Characters/RPGBaseCharacter.h"
ARPGBaseCharacter::ARPGBaseCharacter()
{
PrimaryActorTick.bCanEverTick = false;
PrimaryActorTick.bStartWithTickEnabled = false;
GetMesh()->bReceivesDecals = false;
}