构造函数
Character::Character
在构造的时候被调用
- 声明和获取SkeletalMesh
在头文件中
UProperty(EditInstanceOnly,Category = "Basic Config")
USkeletalMesh* SkeletalMesh;
用SkeletalMesh = LoadObject<USkeletalMesh>(NULL,TEXT(""))
- 设置模型旋转 设置模型坐标 设置骨骼 设置胶囊体
GetMesh()->SetRelativeRotation(FRotator(0,90,0));
GetMesh()->SetRelativeLocation(FVector(0,0,0));
GetMesh()->SetSkeletalMesh(SkeletalMesh); //用上面的变量
GetCapsuleComponent()->InitCapsuleSize(半径,半高);
- 设置相机 相机摇臂(第三人称游戏)
在头文件中
UProperty(EditInstanceOnly,Category = "Basic Config")
USpringArmComponent* SpringArmComponent; //摄像机摇臂
UProperty(EditInstanceOnly,Category = "Basic Config")
UCameraComponent* CameraComponent; //摄像机
void ThirdPerson(); //声明一个第三人称视角的公共函数,以便调用切换视角
在CPP中
SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
- 注册输入事件
void AMainCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// 加两个输入事件控制相机
// 轴运算
PlayerInputComponent -> BindAxis("LookRight", this,&AMainCharacter::AddControllerYawInput);
PlayerInputComponent -> BindAxis("LookForward", this,&AMainCharacter::AddControllerPitchInput);
}
- 第三人称视角
void AMainCharacter::ThirdPerson()
{
bUseControllerRotationPitch = false;
bUseControllerRotationRoll = false;
bUseControllerRotationYaw = false;
GetCharacterMovement() -> RotationRate = FRotator(0,540,0); //相机移动不影响人物模型移动
SpringArmComponent -> SetupAttachment(RootComponent);
SpringArmComponent -> SetRelativeLocation(FVector(0,0,100));
SpringArmComponent -> SetRelativeRotation(FRotator(-15,0,0));
}