应直接继承Character而非Actor创建玩家角色,因其已封装移动组件、碰撞体、动画接口及网络同步能力;从Actor派生需重复实现整套系统,易出错且效率低。
在Unreal Engine中,用C++构建玩家角色的核心实体,关键不是“选Actor还是Character”,而是理解它们的定位与协作关系:Character是专为带移动、碰撞、动画能力的玩家/敌人设计的Actor子类,而Actor是所有可放置对象的基类。直接继承Character才是标准做法。
Actor本身不带移动组件(如UCharacterMovementComponent)、不支持网络同步移动、没有内置的CapsuleCollider、也不提供GetVelocity()、Launch()、AddMovementInput()等角色行为接口。若硬从Actor派生,你得手动添加移动组件、重写Tick逻辑、自行处理地面检测、斜坡滑动、跳跃力计算——等于重复实现Character已封装好的整套物理与输入响应系统。
常见误区示例:
以UE5.3+ C++为例,推荐在编辑器中通过“New C++ Class”向导创建,父类选Character:
// 不要在这里定义UCharacterMovementComponent*,它已在父类中存在 UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Movement") UCapsuleComponent* GetCapsuleComponent() const { return CapsuleComponent; }UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera") USpringArmComponent* CameraBoom;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera") UCameraComponent* FollowCamera;
AMyPlayerCharacter::AMyPlayerCharacter()
{
PrimaryActorTick.bCanEverTick = true;CameraBoom = CreateDefaultSubobject(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->TargetArmLength = 400.f;
CameraBoom->bUsePawnControlRotation = true;
FollowCamera = CreateDefaultSubobject(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->RotationRate = FRotator(0.f, 500.f, 0.f);
GetCharacterMovement()->JumpZVelocity = 700.f;
GetCharacterMovement()->AirControl = 0.35f;}
Character类已预置了输入响应骨架,只需按需重写,无需接管整个Tick循环:
多人游戏中,Character天然适配UE的Replication机制: