【UE4 C++】实现旋转小球的第三人称自由视角

2023-10-12 10:59

本文主要是介绍【UE4 C++】实现旋转小球的第三人称自由视角,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文将介绍用C++实现一个简单的玩家可通过WASD控制移动,Shift进行加速,鼠标控制视角旋转和缩放的小球。

本人也只是一个UE4初学者,大佬勿喷。


一、技术难点

  • 小球通过角速度控制旋转,因此想实现自由视角相机,它就不能作为小球的子物体。
  • 小球的移动方向始终要保持与视野前方相同。

二、最终效果图

 

  • 模型资源链接:https://pan.baidu.com/s/1e2bavPacWwA6_pDHlcM0Tw 密码:na24
  • UE4项目以及源码:https://download.csdn.net/download/qq_31788759/10565311

三、核心代码模块

在此先将模块分类,使结构清晰明了,最后有完整代码,可具体查看。


1、首先创建组件

  • SphereBase.h(自己创建的C++类)下
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "RootComp")class USceneComponent * RootComp;//声明根节点组件UPROPERTY(EditAnywhere, BlueprintReadWrite,Category = "SphereMeshComp")class UStaticMeshComponent * SphereMeshComp;//小球Mesh组件UPROPERTY(EditAnywhere, BlueprintReadWrite,Category = "CameraArmComp")class USpringArmComponent * CameraArmComp;//相机臂组件UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CameraComp")class UCameraComponent * CameraComp;//相机组件
  •  SphereBase.cpp
    //创建组件RootComp = CreateDefaultSubobject<USceneComponent>(TEXT("RootComp"));SphereMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SphereBaseComp"));CameraArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraArmComp"));CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));//添加组件父子关系SphereMeshComp->SetupAttachment(RootComp);CameraArmComp->SetupAttachment(RootComp);CameraComp->SetupAttachment(CameraArmComp);//设置小球物理效果为真SphereMeshComp->SetSimulatePhysics(true);
  • 创建蓝图类继承该C++类后,组件已创建 

2、绑定按键输入

 

    //绑定前后左右移动PlayerInputComponent->BindAxis("MoveForward", this, &ASphereBase::MoveForward);PlayerInputComponent->BindAxis("MoveRight", this, &ASphereBase::MoveRight);//Shift按下与抬起PlayerInputComponent->BindAction("MoveQuick", IE_Pressed, this, &ASphereBase::MoveQuick);
PlayerInputComponent->BindAction("MoveQuick", IE_Released, this, &ASphereBase::MoveNormal);//相机上下左右旋转PlayerInputComponent->BindAxis("CameraYaw", this, &ASphereBase::YawCamera);PlayerInputComponent->BindAxis("CameraPitch", this, &ASphereBase::PitchCamera);//相机缩放PlayerInputComponent->BindAction("ZoomIn", IE_Pressed, this, &ASphereBase::ZoomIn);PlayerInputComponent->BindAction("ZoomIn", IE_Released, this, &ASphereBase::ZoomStop);PlayerInputComponent->BindAction("ZoomOut", IE_Pressed, this, &ASphereBase::ZoomOut);PlayerInputComponent->BindAction("ZoomIn", IE_Released, this, &ASphereBase::ZoomStop);

具体函数请查看完整代码 

3、相机自由视角

建议采用世界坐标系函数修改值,不要使用相对坐标系Relative函数,否则会遇到很多问题。


  • 小球移动控制
    if (!AngularVector.IsZero()){FVector NewVector = FVector(0, 0, 0);NewVector += AngularVector.X  * CameraArmComp->GetForwardVector() * SphereSpeed;NewVector += AngularVector.Y  * CameraArmComp->GetRight	Vector() * SphereSpeed;SphereMeshComp->SetPhysicsAngularVelocity(NewVector);//给小球施加角速度向量}
  • 相机臂左右旋转
    FRotator LRRotation = CameraArmComp->GetComponentRotation();LRRotation.Yaw += CameraInput.X;CameraArmComp->SetWorldRotation(LRRotation);
  • 相机臂上下旋转 
    FRotator UDRotation = CameraArmComp->GetComponentRotation();UDRotation.Pitch = FMath::Clamp(UDRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);//控制上下视野范围CameraArmComp->SetWorldRotation(UDRotation);
  • 相机臂跟随小球
    FVector NewLocation = SphereMeshComp->GetComponentLocation();CameraArmComp->SetWorldLocation(NewLocation);

4、相机缩放 

    ZoomValue = FMath::Clamp<float>(ZoomValue, 0.0f, 1.0f);//基于ZoomFActor来混合控制相机的视域和弹簧臂的长度 0.0f对应90.0f 1500.0fCameraComp->FieldOfView = FMath::Lerp<float>(90.0f, 60.0f, ZoomValue);CameraArmComp->TargetArmLength = FMath::Lerp<float>(1500.0f, 500.0f, ZoomValue);

滚轮控制ZoomValue值的变化

四、完整代码

  •  Sphere.h
#pragma once#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "SphereBase.generated.h"//必须放在头文件最后UCLASS()
class BILICODE_API ASphereBase : public APawn//APawn 继承 Actor
{GENERATED_BODY()public:// Sets default values for this pawn's propertiesASphereBase();UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "RootComp")class USceneComponent * RootComp;UPROPERTY(EditAnywhere, BlueprintReadWrite,Category = "SphereMeshComp")class UStaticMeshComponent * SphereMeshComp;//class声明UPROPERTY(EditAnywhere, BlueprintReadWrite,Category = "CameraArmComp")class USpringArmComponent * CameraArmComp;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CameraComp")class UCameraComponent * CameraComp;public:FVector AngularVector;float SphereSpeed;float SpeedMin;float SpeedMax;FVector CameraInput;float ZoomValue;UPROPERTY(EditAnyWhere, BlueprintReadWrite)bool IsInput;//控制是否能输入按键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;UFUNCTION(BlueprintCallable)void MoveForward(float AxisValue);UFUNCTION(BlueprintCallable)void MoveRight(float AxisValue);UFUNCTION(BlueprintCallable)void MoveQuick();UFUNCTION(BlueprintCallable)void MoveNormal();void PitchCamera(float AxisValue);void YawCamera(float AxisValue);void StartJump();void StopJump();void ZoomIn();void ZoomStop();void ZoomOut();
};
  • Sphere.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include "SphereBase.h"
#include "Components/StaticMeshComponent.h"//Mesh头文件
#include "GameFramework/SpringArmComponent.h"//摄像机手臂头文件
#include "Camera/CameraComponent.h"
#include "Components/SceneComponent.h"
#include "Components/InputComponent.h"//输入按键绑定头文件
#include "Engine.h"// Sets default values
ASphereBase::ASphereBase()
{// 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;IsInput = true;SphereSpeed = 300.0f;SpeedMin = SphereSpeed;SpeedMax = 500.0f;ZoomValue = 0.5f;//创建组件RootComp = CreateDefaultSubobject<USceneComponent>(TEXT("RootComp"));SphereMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SphereBaseComp"));CameraArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraArmComp"));CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));//组件关系SphereMeshComp->SetupAttachment(RootComp);CameraArmComp->SetupAttachment(RootComp);CameraComp->SetupAttachment(CameraArmComp);//设置物理效果为真SphereMeshComp->SetSimulatePhysics(true);
}// Called when the game starts or when spawned
void ASphereBase::BeginPlay()
{Super::BeginPlay();
}// Called every frame
void ASphereBase::Tick(float DeltaTime)
{Super::Tick(DeltaTime);if (!AngularVector.IsZero()){FVector NewVector = FVector(0, 0, 0);NewVector += AngularVector.X  * CameraArmComp->GetForwardVector() * SphereSpeed;NewVector += AngularVector.Y  * CameraArmComp->GetRightVector() * SphereSpeed;SphereMeshComp->SetPhysicsAngularVelocity(NewVector);//小球向一个向量方向旋转移动}{//相机臂左右旋转(相机臂与小球是兄弟关系FRotator LRRotation = CameraArmComp->GetComponentRotation();LRRotation.Yaw += CameraInput.X;CameraArmComp->SetWorldRotation(LRRotation);}{//相机臂跟随小球FVector NewLocation = SphereMeshComp->GetComponentLocation();CameraArmComp->SetWorldLocation(NewLocation);//两种方便的调试方法//GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Purple, NewLocation.ToString());/*DrawDebugLine(GetWorld(),SphereBeginLocation,SphereMeshComp->GetComponentLocation(),FColor::Red,false, -1, 0,3.);*/}{//相机臂上下旋转FRotator UDRotation = CameraArmComp->GetComponentRotation();UDRotation.Pitch = FMath::Clamp(UDRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);//控制旋转范围CameraArmComp->SetWorldRotation(UDRotation);}{//相机缩放ZoomValue = FMath::Clamp<float>(ZoomValue, 0.0f, 1.0f);//基于ZoomFActor来混合相机的视域和弹簧臂的长度CameraComp->FieldOfView = FMath::Lerp<float>(90.0f, 60.0f, ZoomValue);CameraArmComp->TargetArmLength = FMath::Lerp<float>(1500.0f, 500.0f, ZoomValue);}
}// Called to bind functionality to input
void ASphereBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)//pawn不同于actor的地方,用于绑定按键
{Super::SetupPlayerInputComponent(PlayerInputComponent);PlayerInputComponent->BindAxis("MoveForward", this, &ASphereBase::MoveForward);//绑定 前后移动映射 的函数PlayerInputComponent->BindAxis("MoveRight", this, &ASphereBase::MoveRight);//绑定左右PlayerInputComponent->BindAction("MoveQuick", IE_Pressed, this, &ASphereBase::MoveQuick);PlayerInputComponent->BindAction("MoveQuick", IE_Released, this, &ASphereBase::MoveNormal);PlayerInputComponent->BindAxis("CameraYaw", this, &ASphereBase::YawCamera);PlayerInputComponent->BindAxis("CameraPitch", this, &ASphereBase::PitchCamera);PlayerInputComponent->BindAction("ZoomIn", IE_Pressed, this, &ASphereBase::ZoomIn);PlayerInputComponent->BindAction("ZoomIn", IE_Released, this, &ASphereBase::ZoomStop);PlayerInputComponent->BindAction("ZoomOut", IE_Pressed, this, &ASphereBase::ZoomOut);PlayerInputComponent->BindAction("ZoomIn", IE_Released, this, &ASphereBase::ZoomStop);
}//前后左右输入控制
void ASphereBase::MoveForward(float AxisValue)
{if (IsInput){AngularVector.Y = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);}
}void ASphereBase::MoveRight(float AxisValue)
{if (IsInput){AngularVector.X = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);}
}
//shift输入控制
void ASphereBase::MoveQuick()
{SphereSpeed = SpeedMax;
}void ASphereBase::MoveNormal()
{SphereSpeed = SpeedMin;
}
//相机旋转输入控制
void ASphereBase::PitchCamera(float AxisValue)
{CameraInput.Y = AxisValue;
}void ASphereBase::YawCamera(float AxisValue)
{CameraInput.X = AxisValue;
}void ASphereBase::ZoomIn()
{ZoomValue += 0.1f;
}
//相机缩放输入控制
void ASphereBase::ZoomStop()
{
}void ASphereBase::ZoomOut()
{ZoomValue -= 0.1f;
}

 

这篇关于【UE4 C++】实现旋转小球的第三人称自由视角的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/195379

相关文章

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

Nexus安装和启动的实现教程

《Nexus安装和启动的实现教程》:本文主要介绍Nexus安装和启动的实现教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Nexus下载二、Nexus安装和启动三、关闭Nexus总结一、Nexus下载官方下载链接:DownloadWindows系统根

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

MySQL 横向衍生表(Lateral Derived Tables)的实现

《MySQL横向衍生表(LateralDerivedTables)的实现》横向衍生表适用于在需要通过子查询获取中间结果集的场景,相对于普通衍生表,横向衍生表可以引用在其之前出现过的表名,本文就来... 目录一、横向衍生表用法示例1.1 用法示例1.2 使用建议前面我们介绍过mysql中的衍生表(From子句

Mybatis的分页实现方式

《Mybatis的分页实现方式》MyBatis的分页实现方式主要有以下几种,每种方式适用于不同的场景,且在性能、灵活性和代码侵入性上有所差异,对Mybatis的分页实现方式感兴趣的朋友一起看看吧... 目录​1. 原生 SQL 分页(物理分页)​​2. RowBounds 分页(逻辑分页)​​3. Page

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.