前面的文章咱们可以在场景中移动Actor,但是无法移动观察Actor的角度和方位,现在我们要设置可以移动摄像头的方位和观察的角度。

  1. 首先我们在【项目设置】->【输入】添加两个鼠标输入:MousePitch和MouseYaw

    在代码中,绑定这两个输入内容。
 PlayerInputComponent->BindAxis(TEXT("CameraPitch"), this, &AColliderPawn::cameraPitch);PlayerInputComponent->BindAxis(TEXT("CameraYaw"), this, &AColliderPawn::cameraYaw);

记录鼠标移动的数据

void AColliderPawn::cameraPitch(float value) {//UE_LOG(LogTemp, Warning, TEXT("cameraPitch: %f"), value);cameraInput.Y = value;
}void AColliderPawn::cameraYaw(float value) {//UE_LOG(LogTemp, Warning, TEXT("cameraYaw: %f"), value);cameraInput.X = value;
}

然后再Tick函数中实现功能

void AColliderPawn::Tick(float DeltaTime) {Super::Tick(DeltaTime);//左右移动FRotator newRotation = GetActorRotation();newRotation.Yaw += cameraInput.X;SetActorRotation(newRotation);//通过摇臂移动方位FRotator armRotation = mySpringArm->GetComponentRotation();//armRotation.Pitch += cameraInput.Y;armRotation.Pitch = FMath::Clamp((double)(armRotation.Pitch += cameraInput.Y), (double)-80.0, double(-15.0));mySpringArm->SetWorldRotation(armRotation);
}

完整的代码:

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "ColliderPawn.generated.h"UCLASS()
class FIRSTPROJECT_API AColliderPawn : public APawn
{GENERATED_BODY()public:// Sets default values for this pawn's propertiesAColliderPawn();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public: // Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;public://静态网格体UPROPERTY(VisibleAnywhere, Category = "MyMess")UStaticMeshComponent* myMess;FORCEINLINE void setMessComponent(UStaticMeshComponent* mess) {myMess = mess;}FORCEINLINE UStaticMeshComponent* getMessComponent() {return myMess;}//包围球UPROPERTY(VisibleAnywhere, Category = "MyMess")class USphereComponent* mySphereComponent;FORCEINLINE void setSphereComponent(USphereComponent* sphere) {mySphereComponent = sphere;}FORCEINLINE USphereComponent* getSphereComponent() {return mySphereComponent;}//摄像机UPROPERTY(VisibleAnywhere, Category = "MyMess")class UCameraComponent* myCamera;FORCEINLINE void setCameraComponent(UCameraComponent* camera) {myCamera = camera;}FORCEINLINE UCameraComponent* getCameraComponent() {return myCamera;}//摇臂UPROPERTY(VisibleAnywhere, Category = "MyMess")class USpringArmComponent* mySpringArm;FORCEINLINE void setSpringArmComponent(USpringArmComponent* arm) {mySpringArm = arm;}FORCEINLINE USpringArmComponent* getSpringArmComponent() {return mySpringArm;}//移动组件UPROPERTY(VisibleAnywhere, Category = "MyMess")class UColliderMovementComponent* myMovementComponent;virtual UPawnMovementComponent* GetMovementComponent()const override;private:void moveForward(float value);void moveRight(float value);void cameraPitch(float value);void cameraYaw(float value);private:FVector2D cameraInput;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "ColliderPawn.h"
#include "Components/SphereComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "ColliderMovementComponent.h"// Sets default values
AColliderPawn::AColliderPawn() {// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = true;RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));mySphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("MySphere"));mySphereComponent->InitSphereRadius(40.0);mySphereComponent->SetCollisionProfileName(TEXT("Pawn"));mySphereComponent->SetupAttachment(GetRootComponent());myMess = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMess"));myMess->SetupAttachment(GetRootComponent());//通过代码的方式设置静态网格体static ConstructorHelpers::FObjectFinder<UStaticMesh> messAsset(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'"));if (messAsset.Succeeded()){myMess->SetStaticMesh(messAsset.Object);myMess->SetRelativeLocation(FVector(0.0, 0.0, -40.0));myMess->SetWorldScale3D(FVector(0.8));}mySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArm"));mySpringArm->SetupAttachment(GetRootComponent());mySpringArm->SetRelativeRotation(FRotator(-45.0, 0.0, 0.0));mySpringArm->TargetArmLength = 400.0;mySpringArm->bEnableCameraLag = true;mySpringArm->CameraLagSpeed = 3.0;myCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));myCamera->SetupAttachment(mySpringArm, USpringArmComponent::SocketName);myMovementComponent = CreateDefaultSubobject<UColliderMovementComponent>(TEXT("MyMovementCompoment"));myMovementComponent->UpdatedComponent = RootComponent;cameraInput = FVector2D(0.0);//自动支配AutoPossessPlayer = EAutoReceiveInput::Player0;
}// Called when the game starts or when spawned
void AColliderPawn::BeginPlay() {Super::BeginPlay();
}// Called every frame
void AColliderPawn::Tick(float DeltaTime) {Super::Tick(DeltaTime);//左右移动FRotator newRotation = GetActorRotation();newRotation.Yaw += cameraInput.X;SetActorRotation(newRotation);//通过摇臂移动方位FRotator armRotation = mySpringArm->GetComponentRotation();//armRotation.Pitch += cameraInput.Y;armRotation.Pitch = FMath::Clamp((double)(armRotation.Pitch += cameraInput.Y), (double)-80.0, double(-15.0));mySpringArm->SetWorldRotation(armRotation);
}// Called to bind functionality to input
void AColliderPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {Super::SetupPlayerInputComponent(PlayerInputComponent);PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &AColliderPawn::moveForward);PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &AColliderPawn::moveRight);PlayerInputComponent->BindAxis(TEXT("CameraPitch"), this, &AColliderPawn::cameraPitch);PlayerInputComponent->BindAxis(TEXT("CameraYaw"), this, &AColliderPawn::cameraYaw);
}UPawnMovementComponent* AColliderPawn::GetMovementComponent()const {return myMovementComponent;
}void AColliderPawn::moveForward(float value) {FVector forword = GetActorForwardVector();if (myMovementComponent){myMovementComponent->AddInputVector(forword * value);}
}void AColliderPawn::moveRight(float value) {FVector right = GetActorRightVector();if (myMovementComponent) {myMovementComponent->AddInputVector(right * value);}
}void AColliderPawn::cameraPitch(float value) {//UE_LOG(LogTemp, Warning, TEXT("cameraPitch: %f"), value);cameraInput.Y = value;
}void AColliderPawn::cameraYaw(float value) {//UE_LOG(LogTemp, Warning, TEXT("cameraYaw: %f"), value);cameraInput.X = value;
}

aaa

UE4通过鼠标在pawn四周移动摄像头相关推荐

  1. UE4中Actor、Pawn、Character等各种类的详细了解。

    1.Actor Actor类是可以放到游戏场景中的游戏对象的基本类型.你如果想放置任何东西到游戏场景中,必须继承Actor类.(类似Unity中的GameObject) 2.Pawn Actor 的一 ...

  2. UE4 用C++让Pawn动起来

    用C++让Pawn动起来 首先新建一个C++项目 打开C++类,然后新建一个C++类 选择Pawn 设置一个类名,并且可以选择新建一个文件夹 创建完成后就会打开VisualStudio 会看到有两个文 ...

  3. UE4学习-鼠标事件(按下、释放、物体抓取、计算重量、触发开门)

    文章目录 关键类 鼠标按下.释放事件 物体抓取 计算物体重量 关键类 本篇博文用到的关键类有: UInputComponent 用来绑定鼠标的按下和释放事件 BindAction UPhysicsHa ...

  4. 【虚幻引擎】UE4/UE5鼠标点击事件实现物体移动

    B站教学链接:https://space.bilibili.com/449549424?spm_id_from=333.1007.0.0 一.原理解析 在UE4/UE5中,引擎有它自己的一套框架体系, ...

  5. UE4通过鼠标和键盘可以移动角色

    按键[W][S][A][D]实现上下左右移动,[空格]跳跃 移动鼠标实现方向的移动 设置按键映射 代码 // Fill out your copyright notice in the Descrip ...

  6. UE4 自定义鼠标样式

    主要内容: 在项目制作中我们往往不会使用默认的鼠标样式,这时就需要自定义鼠标样式,具体实现步骤就是创建一个带有图片的UI蓝图然后在项目设置里的UserInterface里进行设置. 实现步骤: 1.新 ...

  7. Ue4制作鼠标拖尾效果

    开启UE中的Niagara UI Renderer插件(所有兼容这个插件的UE版本中都有自带,如果没有可以去下载,免费的) 2.根据图提示,新建一个UI例子特效,用来作为拖尾 3.新建一个UMG,创建 ...

  8. [UE4]网游中角色Pawn的移动位置同步以及RTS多角色同时移动的解决方案

    下面方案的思路是: 每个Actor,为其定义一个代理(ActorProxy),真实的Actor放在服务端,代理ActorProxy放在客户端,移动Actor时,实际是移动服务端上的Actor,然后对客 ...

  9. UE4鼠标滚轮控制镜头缩放

    蓝图: 因为其实实现起来比较简单,所以直接上蓝图: 主要是用到了UE4的鼠标滚轮操作映射,每当滑动鼠标滚轮的时候就会传出一个数值,有正有负有0,然后将角色的相机摇臂组件拖进蓝图,获取其中的Target ...

最新文章

  1. 数组随机抽取 java_Java利用数组随机抽取幸运观众如何实现
  2. at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:)
  3. Validation of ViewState Mac failed exception
  4. 代码高亮_微信公众号代码高亮美化工具 Markdown Nice
  5. 抓取标准报表ALV GRID上的数据
  6. 2d绘制 c# dx_C# DX 编程
  7. 设置SQLServer数据库内存
  8. ionic4页面常用判断
  9. Lateral View使用指南
  10. 手把手教,使用Oracle VM VirtualBox虚拟机安装Windows XP系统,爷青回
  11. 通俗易懂的数学建模示例(一)
  12. 爱特php文件管理器2.8_爱特全能网站文件专家
  13. JavaGuide--Java篇
  14. 华东理工大学考研计算机难度,华东理工大学(专业学位)计算机技术考研难吗
  15. 加工制造业经销商渠道管理系统:共享上下游信息,加速交易效率
  16. R语言实现分层抽样(Stratified Sampling)以iris数据集为例
  17. 把C盘正好分成100G的数值
  18. CSS:“ ”这个符号在css中一般用 arial字体
  19. pytorch中repeat()函数理解
  20. 推荐几款实用软件工具

热门文章

  1. 直观理解高斯函数相乘
  2. 计算机高校挑战赛英语,2019全国高校计算机能力挑战赛
  3. 高通与三星延长移动技术专利许可协议;壳牌与申能将在中国共同建设加氢站网络 | 美通企业日报...
  4. B站英语情景剧 P12
  5. 【HDMI】petalinux2022中digilent_encoder hdmi驱动不在适用
  6. 联想 NetApp 数据管理解决方案
  7. Android蓝牙自拍杆按钮防抖
  8. 前端知识点(1)——preventDefault和stopPropagation
  9. 两顺序栈共享Java_数据结构与算法(三),栈与队列
  10. css position的相关属性: