1. 新建一个Character C++类
默认包含:
1 //构造函数 2 ATheCharacter(); 3 4 //开始 5 virtual void BeginPlay() override; 6 7 //更新 8 virtual void Tick(float DeltaTime) override; 9 10 //输入组件 11 virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
2. 绑定相机和弹簧臂
为了能正常看到玩家,需要绑定相机和弹簧臂,弹簧臂绑定在人物胶囊体上,相机绑定在弹簧臂上。
UPROPERTY(VisibleAnywhere) class USpringArmComponent* SpringArmComp; UPROPERTY(VisibleAnywhere) class UCameraComponent* CameraComp;
1 #include <GameFramework/SpringArmComponent.h> 2 #include <Camera/CameraComponent.h> 3 4 // Sets default values 5 ATheCharacter::ATheCharacter() 6 { 7 // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it. 8 PrimaryActorTick.bCanEverTick = true; 9 10 SpringArmComp = CreateDefaultSubobject<USpringArmComponent>("SpringArmComp"); 11 SpringArmComp->SetupAttachment(RootComponent); 12 13 CameraComp = CreateDefaultSubobject<UCameraComponent>("CameraComp"); 14 CameraComp->SetupAttachment(SpringArmComp); 15 }
-------------------------------------------
transient property???
Property is transient, meaning it will not be saved or loaded.
Properties tagged this way will be zero-filled at load time.
SetupAttachment 和 AttachToComponent的区别?
都是场景组件的附加方法。前者在构造时调用,后者在游戏中调用。
3. 添加移动组件
然后让玩家可以走路,转向
1 void ATheCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) 2 { 3 Super::SetupPlayerInputComponent(PlayerInputComponent); 4 5 PlayerInputComponent->BindAxis("MoveForward", this, &ATheCharacter::MoveForward); 6 PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput); 7 }
void ATheCharacter::MoveForward(float value) { AddMovementInput(GetActorForwardVector(), value); }
别忘了头文件声明方法:void MoveForward(float value);
pitch是围绕X轴旋转,也叫做俯仰角
yaw是围绕Y轴旋转,也叫偏航角
roll是围绕Z轴旋转,也叫翻滚角
标签:Engine,ATheCharacter,void,Character,CameraComp,Unreal,PlayerInputComponent,Sprin From: https://www.cnblogs.com/tomatokely/p/17114900.html