1首先创建一个第三人称游戏的C++项目
2在myprojectCharacter.h中添加
/** First person camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
UCameraComponent* FirstPersonCameraComponent;
/** Pawn mesh: 1st person view (arms; seen only by self) */
UPROPERTY(VisibleDefaultsOnly, Category=Mesh)
USkeletalMeshComponent* Mesh1P;
```/** Returns Mesh1P subobject **/
USkeletalMeshComponent* GetMesh1P() const ;
/** Returns FirstPersonCameraComponent subobject **/
UCameraComponent* GetFirstPersonCameraComponent() const ;
/** Bool for AnimBP to switch to another animation set */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Weapon)
bool bHasRifle;
/** Setter to set the bool */
UFUNCTION(BlueprintCallable, Category = Weapon)
void SetHasRifle(bool bNewHasRifle);
/** Getter for the bool */
UFUNCTION(BlueprintCallable, Category = Weapon)
bool GetHasRifle();
在myprojectCharacter.cpp中添加:
// Create a CameraComponent
FirstPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
FirstPersonCameraComponent->SetupAttachment(GetCapsuleComponent());
FirstPersonCameraComponent->SetRelativeLocation(FVector(-10.f, 0.f, 60.f)); // Position the camera
FirstPersonCameraComponent->bUsePawnControlRotation = true;
// Create a mesh component that will be used when being viewed from a '1st person' view (when controlling this pawn)
Mesh1P = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("CharacterMesh1P"));
Mesh1P->SetOnlyOwnerSee(true);
Mesh1P->SetupAttachment(FirstPersonCameraComponent);
Mesh1P->bCastDynamicShadow = false;
Mesh1P->CastShadow = false;
//Mesh1P->SetRelativeRotation(FRotator(0.9f, -19.19f, 5.2f));
Mesh1P->SetRelativeLocation(FVector(-30.f, 0.f, -150.f));
// Character doesnt have a rifle at start
bHasRifle = false;
···································
USkeletalMeshComponent* AmyprojectCharacter::GetMesh1P() const
{ return Mesh1P; }
UCameraComponent* AmyprojectCharacter::GetFirstPersonCameraComponent() const
{ return FirstPersonCameraComponent; }
void AmyprojectCharacter::SetHasRifle(bool bNewHasRifle)
{
bHasRifle = bNewHasRifle;
}
bool AmyprojectCharacter::GetHasRifle()
{
return bHasRifle;
}
2.双击人物蓝图
3.蓝图如下
4保存编译
5构建,构建所有关卡