首页 > 其他分享 >UE4添加人物摄像机

UE4添加人物摄像机

时间:2023-06-01 15:23:29浏览次数:39  
标签:CameraComponent void 摄像机 添加 组件 UE4 SpringArmComponent include

在这一节中,我们需要添加两个组件分别是摄像机弹簧臂组件和摄像机组件。

摄像机弹簧臂组件

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

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. }

标签:CameraComponent,void,摄像机,添加,组件,UE4,SpringArmComponent,include
From: https://www.cnblogs.com/tomato-haha/p/17449088.html

相关文章

  • UE4添加人物模型
    在正式使用UE4添加人物模型之前,我们先来解释几个概念。什么是组件在添加人物模型之前先介绍一个概念叫“组件”,组件是什么呢?组件的作用是为了让Actor实现一个功能,比如说:我们现在的角色,没有模型,我们需要一个模型那怎么办呢?那么我就要在Character身上挂一个“骨骼模型组件”......
  • UE4实现人物跳跃
    这一节我们来实现人物的跳跃。1)首先我们打开UE4编辑器,点击项目设置,点击输入,添加BindAction类型的按键绑定,名字为Jump,按键是空格键。2)打开VS编辑器,在我们的角色类APlayingCharacter头文件的末尾声明两个函数,分别是开始跳跃和停止跳跃:复制纯文本复制//......
  • UE4设置人物移动和人物视角
    本节我们先讲解UE4人物移动的设置,然后再讲解UE4人物视角的设置。UE4设置人物移动1)打开UE4编辑器,点击“编辑”然后点击“项目设置”:2)选择“输入”:3)点击“AxisMappins”的添加按键输入,我们创建6个按键输入:4)更改按键和按键的名字,这里一个按键......
  • UE4新建项目
    打开UE4启动器,选择4.21.2版本的虚幻引擎,点击“启动”按钮。 点击之后会出现如下图所示的界面,可以对新建项目进行设置。 “项目”这一栏存放着你新建后的项目。我们点击“新建项目”,点击“C++”,选择“基础代码”,选择“没有初学者内容”,然后写上项目名字,注意不......
  • UE4使用什么编译器?
    UE4的底层虽然是用C++实现的,但它又不仅仅是一个C++库,它还实现了自己的编译机制。换句话说,UE4对现有的C++语法进行了改进,以适应游戏开发的场景。当然,UE4也不会疯狂到自己去开发一个C++编译器,它还是需要依赖传统的C++编译器以及相关的编程套件。熟悉Qt的读者应该......
  • UE4是什么?虚幻4引擎是什么?
    UE4的全名是UnrealEngine4,中文译为“虚幻引擎4”。UE4是一款由EpicGames公司开发的开源、商业收费、学习免费的游戏引擎。从1998年发行至今,UE4一共经历了UE、UE2、UE2.5、UE3、UDK、UE4多个版本,它的迭代速度极快,现在已经到了4.22预览版。基于UE4开发的大作无......
  • UE4的下载和安装(UE4开发环境的搭建)
    使用UE4开发游戏之前,必须先搭建开发环境,具体包括:UE4引擎的下载和安装VisualStudio下载和安装UE4的下载和安装1)首先,打开任意一个浏览器输入UE4官方网站的网址 https://www.unrealengine.com/zh-CN/,点击右上角的“下载”按钮,如下图所示:2)官方要求必须登录......
  • Burp Suite添加https证书——让其可以嗅探https的数据包,本质上就是将BP自己做为https
    如何给软件BurpSuite添加https证书?BurpSuite是一款强大的安全测试工具,可以用来设置代理,抓取http数据包,如果添加了https证书,就可以抓取https数据包。这边经验就告诉你,如何给软件BurpSuite添加https证书。工具/原料 联网的电脑一台BurpSuite软件firefox浏览器,并安装proxyswitch......
  • windows下通过net user add和powershell添加用户,sysmon仅仅采集到进程,而在windows安全
    执行操作:C:\Windows\system32>netuser/add"jack""fuckoff"命令成功完成。C:\Windows\system32>powershellWindowsPowerShell版权所有(C)MicrosoftCorporation。保留所有权利。尝试新的跨平台PowerShellhttps://aka.ms/pscore6PSC:\Windows\system32&g......
  • 原生JS输入姓名科目分数添加到表格,可删除
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=d......