【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

相关文章

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

Python实现精确小数计算的完全指南

《Python实现精确小数计算的完全指南》在金融计算、科学实验和工程领域,浮点数精度问题一直是开发者面临的重大挑战,本文将深入解析Python精确小数计算技术体系,感兴趣的小伙伴可以了解一下... 目录引言:小数精度问题的核心挑战一、浮点数精度问题分析1.1 浮点数精度陷阱1.2 浮点数误差来源二、基础解决

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo