当前位置: 首页 > news >正文

UE5 C++ 创建可缩放的相机

一.要将相机设置在Pawn类里

1.在MyPawn头文件里,加上摇臂和相机组件

#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"

2.在Pawm里声明SceneComponet,SpringArmComponent,CameraComponent组件指针

再声明一个移动缩放调用的函数

public:UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "MySceneComponent")USceneComponent* MyRoot;UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "MySceneComponent")USpringArmComponent* MySpringArm;UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "MySceneComponent")UCameraComponent* MyCamera;//鼠标划轮移动镜头缩放void Zoom(bool Direction,float ZoomSpeed);

在Pawn里将的组件通过CreateDefaultSubobject<T>(TEXT("Name"))创造命名。

根组件赋值为MyRoot的物体组件。SetupAttackment来连接子组件。将MySpringArm的bDoCollisionTest = false来停止碰撞。

AMyPawn::AMyPawn()
{// 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;MyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("MyRootComponent"));MySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArmComponent"));MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCameraComponent"));RootComponent = MyRoot;MySpringArm->SetupAttachment(MyRoot);MyCamera->SetupAttachment(MySpringArm);MySpringArm->bDoCollisionTest = false;  //让它无碰撞

3.设置好Pawn的Transform信息。

	FVector MyLocation = FVector(0,0,0);FRotator MyRotation = FRotator(-50,0,0);FVector MyScale = FVector(1,1,1);SetActorLocation(MyLocation);SetActorRotation(MyRotation);SetActorScale3D(MyScale);

4.在滑动函数里,将相机的伸缩臂的伸缩方向和速度的逻辑写好

void AMyPawn::Zoom(bool Direction, float ZoomSpeed)
{if (Direction) //1{if (MySpringArm->TargetArmLength >= 300 && MySpringArm->TargetArmLength < 5000)  //如果摄像机摇臂在300 到 5000之间{MySpringArm->TargetArmLength += (ZoomSpeed * 2);GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("SpringArmLength is %f"), MySpringArm->TargetArmLength));}}else{if (MySpringArm->TargetArmLength > 300 && MySpringArm->TargetArmLength <= 5000){MySpringArm->TargetArmLength -= (ZoomSpeed * 2);GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,FString::Printf(TEXT("SpringArmLength is %f"),MySpringArm->TargetArmLength));}}
}

5.在PlayerController.cpp头文件里包含Pawn。注意顺序(MyPlayerController.h要放在第一个)

#include "MyPlayerController.h"
#include "MyPawn.h"

在PlayerController.h里声明 绑定输入 和 其他功能函数

	virtual void SetupInputComponent();void WheelUpFunction();void WheelDownFunction();

设置输入绑定事件,由于要重写SetupInputComponent虚函数,首先要继承父类的Super::SetupInputComponent(); 这个在UE里要写。再加上额外添加的代理绑定功能。

通过绑定输入代理,调用PlayerController的新定义的函数

void AMyPlayerController::SetupInputComponent()
{Super::SetupInputComponent();InputComponent->BindAction("WheelUp",IE_Pressed,this,&AMyPlayerController::WheelUpFunction);InputComponent->BindAction("WheelDown",IE_Pressed,this,&AMyPlayerController::WheelDownFunction);
}

新定义的函数中,通过GetPawn()拿到,GameMode里的Pawn,把它转换为AMyPawn。如果转换成功,在调用里面的MyPawn里的Zoom函数。

void AMyPlayerController::WheelUpFunction()
{if (GetPawn()){AMyPawn* MyCameraPawn = Cast<AMyPawn>(GetPawn());if (MyCameraPawn){MyCameraPawn->Zoom(true, 10);}}
}void AMyPlayerController::WheelDownFunction()
{if (GetPawn()){AMyPawn* MyCameraPawn = Cast<AMyPawn>(GetPawn());if (MyCameraPawn){MyCameraPawn->Zoom(false, 10);}}
}

可以伸缩,并且打印SpringArm的长度

http://www.lryc.cn/news/303507.html

相关文章:

  • Fabric中的溯源方法
  • 混子文章|蓝桥杯一题 -平方差
  • 计算机视觉基础:【矩阵】矩阵选取子集
  • 解决laravel-admin安装报错1071 Specified key was too long问题
  • 【Python---六大数据结构】
  • 一个简短的补充------对链表练习题的补充补充
  • Spring最新核心高频面试题(持续更新)
  • [计网底层小探索]:实现并部署多线程并发Tcp服务器框架(基于生产者消费者模型的线程池结构)
  • Spring Boot 笔记 020 redis集成
  • 防火墙——计算机网络
  • 用html编写的招聘简历
  • 215数组中的第K个最大元素
  • 【动态规划】【矩阵快速幂】LeetCode2851. 字符串转换
  • 【LeetCode每日一题】单调栈 402 移掉k位数字
  • 力扣 309. 买卖股票的最佳时机含冷冻期
  • 2024年刷题记录
  • 【JGit 】简述及学习资料整理
  • python数据类型-集合set
  • excel如何指定求和
  • 服务端实时推送技术之SSE(Server-Send Events)
  • 使用IntelliJ IDEA查看接口的全部实现方法
  • 阿里云幻兽帕鲁服务器操作系统类型怎么选择?
  • Codeforces Round 927 (Div. 3) LR-remainders的题解
  • HarmonyOS—@Observed装饰器和@ObjectLink嵌套类对象属性变化
  • The method toList() is undefined for the type Stream
  • vue+element (el-progress)标签 隐藏百分比(%) ,反向显示 ,自定义颜色, demo 复制粘贴拿去用
  • Android轻量级进程间通信Messenger源码分析
  • C#开发AGV地图编辑软件
  • 嵌入式学习day22 Linux
  • 不确定性问题的论文笔记