UE4添加人物摄像机

发布时间 2023-06-01 15:18:42作者: tomato-haha
在这一节中,我们需要添加两个组件分别是摄像机弹簧臂组件和摄像机组件。

摄像机弹簧臂组件

摄像机弹簧臂组件,可以想象成是我们的手臂和手。手拿着摄像机,当我们想移动摄像机的时候,我们移动的是我们的手臂而不是摄像机。

1) 打开VS编辑器,在PlayingCharacter.h文件添加摄像机弹簧组件和摄像机组件。
  1. //摄像机弹簧臂组件
  2. class USpringArmComponent* SpringArmComponent;
  3. //摄像机组件
  4. class UCameraComponent* CameraComponent;

2) 在CPP文件中,添加两个头文件和在构造函数中注册这两个组件。
  1. #include "GameFramework/SpringArmComponent.h"
  2. #include "Camera/CameraComponent.h"
  3.  
  4. //注册摄像机手臂组件
  5. SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
  6. //把这个组件绑定到根组件
  7. SpringArmComponent->SetupAttachment(RootComponent);
  8. //设置摄像机手臂和根组件之间的距离
  9. SpringArmComponent->TargetArmLength = 300.0f;
  10. //我们使用模型组件去进行旋转,如果不设置设个的话,Pitch轴无法进行视角移动
  11. SpringArmComponent->bUsePawnControlRotation = true;
  12.  
  13. //注册摄像机组件
  14. CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CaameraComponent"));
  15. //把摄像机绑定到摄像机手臂上
  16. CameraComponent->SetupAttachment(SpringArmComponent);



3) 由于我们的骨骼模型生成的时候它的位置和旋转是不对的,所以我们要设置一下我们骨骼模型的位置和旋转。

  1. //设置模型位置,这里我们把人物以Z轴移动了90个单位,也就向下移动了90个单位
  2. GetMesh()->SetRelativeLocation(FVector(0,0,-90));
  3. //设置模型面朝方向
  4. GetMesh()->SetRelativeRotation(FRotator(0, -90,0));

编译一下,回到编辑器中,发现我们的摄像机已经添加了进去。
UE4摄像机已经添加进去

完整代码如下。

1) PlayingCharacter.h
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3.  
  4. #include "CoreMinimal.h"
  5. #include "GameFramework/Character.h"
  6. #include "PlayingCharacter.generated.h"
  7.  
  8. UCLASS()
  9. class GAMEPROJECT_API APlayingCharacter : public ACharacter
  10. {
  11. GENERATED_BODY()
  12.  
  13. public:
  14. // Sets default values for this character's properties
  15. APlayingCharacter();
  16.  
  17. protected:
  18. // Called when the game starts or when spawned
  19. virtual void BeginPlay() override;
  20.  
  21. public:
  22. // Called to bind functionality to input
  23. virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
  24.  
  25. void MoveForward(float val); //人物往前移动
  26. void MoveBack(float val); //人物向后
  27. void MoveRight(float val); //人物向右
  28. void MoveLeft(float val); //人物向左
  29.  
  30. //跳跃开始
  31. void JumpStart();
  32. //跳跃结束
  33. void JumpEnd();
  34.  
  35. private:
  36. //这个是骨骼模型
  37. USkeletalMesh* SkeletalMesh = nullptr;
  38. //摄像机弹簧臂组件
  39. class USpringArmComponent* SpringArmComponent;
  40. //摄像机组件
  41. class UCameraComponent* CameraComponent;
  42. };



2) PlayingCharacter.cpp

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "PlayingCharacter.h"
  4. #include "GameFramework/SpringArmComponent.h"
  5. #include "Camera/CameraComponent.h"
  6.  
  7. // Sets default values
  8. APlayingCharacter::APlayingCharacter()
  9. {
  10.  
  11. SkeletalMesh = CreateDefaultSubobject<USkeletalMesh>(TEXT("SkeletalMesh"));
  12.  
  13. //加载模型
  14. SkeletalMesh = LoadObject<USkeletalMesh>(NULL,TEXT("SkeletalMesh'/Game/TwinSwordAnimsetBase/UE4_Mannequin/Mesh/SK_Mannequin.SK_Mannequin'"));
  15.  
  16. //把我们的模型赋值到模型组件
  17. GetMesh()->SetSkeletalMesh(SkeletalMesh);
  18. //设置模型位置
  19. GetMesh()->SetRelativeLocation(FVector(0,0,-90));
  20. //设置模型面朝方向
  21. GetMesh()->SetRelativeRotation(FRotator(0, -90,0));
  22.  
  23. //注册摄像机手臂组件
  24. SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
  25. //把这个组件绑定到根组件
  26. SpringArmComponent->SetupAttachment(RootComponent);
  27. //设置摄像机手臂和根组件之间的距离
  28. SpringArmComponent->TargetArmLength = 300.0f;
  29. //我们使用模型组件去进行旋转,如果不设置设个的话,Pitch轴无法进行视角移动
  30. SpringArmComponent->bUsePawnControlRotation = true;
  31.  
  32. //注册摄像机组件
  33. CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CaameraComponent"));
  34. //把摄像机绑定到摄像机手臂上
  35. CameraComponent->SetupAttachment(SpringArmComponent);
  36. }