UE4通过鼠标在pawn四周移动摄像头

news/2024/11/7 17:59:13/

前面的文章咱们可以在场景中移动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


http://www.ppmy.cn/news/206459.html

相关文章

ue4c++日记6(操控一个character类 )

目录 操控一个character类 头文件 cpp文件 操作映射 个人体会 1.角色移动 2.镜头上下左右移动 运行中显示碰撞 运行- 按“~”键&#xff0c;即打开控制台&#xff0c;输入show collision。 操控一个character类 创建一个c类&#xff0c;继承character-&#xff08;公…

从虚拟摄像到异构计算 IT成就魔幻《阿凡达》

片长160分钟、历时14年打造、成本超过二十亿元、3000多个特效镜头、2000名幕后工作人员中800个为CGI特效人员……这些让人惊叹的数字全部出自最近最火爆的3D大片《阿凡达》。这部由《泰坦尼克号》导演詹姆斯卡梅隆重磅出击的3D大制作几乎每分钟都相当精彩&#xff0c;而由于片长…

无人机航模新手100 问

原文地址&#xff1a; 无人机航模新手100问 作者&#xff1a; 春风慕雪 ​ 1.新手入门须知 答&#xff1a;首先根据个人的爱好、动手能力、财力水平做一个自我评价&#xff0c;决定入手航模的方向&#xff0c;如固定翼、直机、多轴&#xff0c;固定翼入门较为简单&#xff0c;直…

誉沃虚拟演播室系统

标题 誉沃虚拟演播室系统 虚拟演播室系统&#xff0c;包括控制室在内&#xff0c;是制作电视节目必不可少的工作环境&#xff0c;是数字化校园电视台的前端&#xff0c;要想制成高质量的教学片&#xff0c;必须具备在照明、声学等方面满足拍摄电视节目的环境以及先进的编辑过程…

虚幻动画 | 让角色动起来,实现一个简易的走、跑、跳状态机

本篇内容将简单介绍如何借助蓝图控制角色&#xff0c;利用现成的素材&#xff0c;从0到1实现一个简易的"走、跑、跳"状态机。目的是让新手更快速清晰地了解虚幻动画系统的运作流程&#xff0c;因此涉及到的内容也比较简单&#xff0c;作为刚开始入门了解虚幻动画的起…

AI产品经理成长路

AI产品经理——成长路 https://www.cnblogs.com/DicksonJYL/p/9566740.html 以下都是自己平时知识的一些总结&#xff0c;只是一些个人的愚见&#xff0c;下面出现的公司、书籍、视频、网站都是自己看过体验过的&#xff0c;不是给他们打广告&#xff0c;不是广告&#xff01;不…

虚拟演播室建设--这篇文章或许能用上

现在有很多电视台、企业单位及个人业主&#xff0c;由于业务要求&#xff0c;都会建设自己的演播室&#xff0c;但是大家都不知道如何着手去做&#xff0c;甚至业主自己都不太清楚具体的规划&#xff0c;导致了很多业主&#xff0c;花了很多钱制作了自己的演播室&#xff0c;不…

UE Gameplay入门51(相机视锥空间的计算方法推导)

非常感谢匿名大哥一直对我的支持&#xff0c;本文内容由他赞助 #1. 视锥&#xff08;Frustum&#xff09;是什么 在相机的近裁剪面和远裁剪面之间的渲染范围内的空间叫做视锥空间&#xff08;Frustum&#xff09;&#xff0c;通常情况下我们是不需要处理&#xff0c;但 当下比…