发布时间:2026/8/3 9:02:46
UEC++ 创建自定义类型案例 一子弹类型// .h文件 #include CoreMinimal.h #include GameFramework/Actor.h #include Components/SphereComponent.h #include GameFrameWork/ProjectileMovementComponent.h #include MyActor.generated.h // ... UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUStaticMeshComponent Sphere; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUSphereComponent SphereCollision; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUProjectileMovementComponent ProjectileMovement;// .cpp文件 AMyActor::AMyActor() { PrimaryActorTick.bCanEverTick true; Sphere CreateDefaultSubobjectUStaticMeshComponent(TEXT(Sphere)); SphereCollision CreateDefaultSubobjectUSphereComponent(TEXT(SphereCollision)); ProjectileMovement CreateDefaultSubobjectUProjectileMovementComponent(TEXT(ProjectileMovement)); static ConstructorHelpers::FObjectFinderUStaticMesh StaticSphereMesh(TEXT(/Engine/BasicShapes/Sphere.Sphere)); RootComponent Sphere; SphereCollision-SetupAttachment(RootComponent); ProjectileMovement-SetUpdatedComponent(RootComponent); Sphere-SetStaticMesh(StaticSphereMesh.Object); ProjectileMovement-InitialSpeed 500.0; ProjectileMovement-MaxSpeed 2000.0; }二带有增强输入的角色类型// .h文件 #include CoreMinimal.h #include InputActionValue.h #include EnhancedInputComponent.h #include EnhancedInputSubsystems.h #include GameFramework/Controller.h #include GameFramework/SpringArmComponent.h #include Camera/CameraComponent.h #include GameFramework/Character.h #include GameFramework/CharacterMovementComponent.h #include MyCharacter.generated.h class UInputAction; class UInputMappingContext; // ... public: UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUSpringArmComponent SpringArm; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUCameraComponent Camera; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUInputMappingContext DefaultMappingContext; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUInputAction MoveAction; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtrUInputAction LookAction; public: void Move(const FInputActionValue value); void Look(const FInputActionValue value); // ...// .cpp文件 AMyCharacter::AMyCharacter() { PrimaryActorTick.bCanEverTick true; SpringArm CreateDefaultSubobjectUSpringArmComponent(TEXT(SpringArm)); Camera CreateDefaultSubobjectUCameraComponent(TEXT(Camera)); SpringArm-TargetArmLength 400.0f; SpringArm-bUsePawnControlRotation true; SpringArm-SetupAttachment(RootComponent); Camera-SetupAttachment(SpringArm); //控制器只影响相机不影响角色 bUseControllerRotationPitch false; bUseControllerRotationRoll false; bUseControllerRotationYaw false; GetCharacterMovement()-bOrientRotationToMovement true; } void AMyCharacter::BeginPlay() { Super::BeginPlay(); if (auto PlayerController CastAPlayerController(Controller)) if (auto Subsystem ULocalPlayer::GetSubsystemUEnhancedInputLocalPlayerSubsystem(PlayerController-GetLocalPlayer())) Subsystem-AddMappingContext(DefaultMappingContext, 0); } void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); if (auto EnhancedInputComponent CastCheckedUEnhancedInputComponent(PlayerInputComponent)) { EnhancedInputComponent-BindAction(MoveAction, ETriggerEvent::Triggered, this, AMyCharacter::Move); EnhancedInputComponent-BindAction(LookAction, ETriggerEvent::Triggered, this, AMyCharacter::Look); } } void AMyCharacter::Move(const FInputActionValue value) { FVector2D MovementVector value.GetFVector2D(); if (Controller) { const FRotator Rotation Controller-GetControlRotation(); const FRotator YawRotation(0, Rotation.Yaw, 0); const FVector FowardDirection FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X); const FVector RightDirection FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y); AddMovementInput(FowardDirection, MovementVector.Y); AddMovementInput(RightDirection, MovementVector.X); } } // 5.7使用IMC_Default 和 IMC_MouseLook 将按键和鼠标操作分开 // IMC_Default → 按键操作移动、跳跃、冲刺、交互等 // IMC_MouseLook → 鼠标操作视角旋转 void AMyCharacter::Look(const FInputActionValue value) { FVector2D LookAxisVector value.GetFVector2D(); if (Controller) { AddControllerYawInput(LookAxisVector.X); AddControllerPitchInput(LookAxisVector.Y); } }

相关新闻

2026/8/3 9:02:46

智谱 GLM Coding Plan 2026年7月31日套餐变动分析报告

智谱 GLM Coding Plan 2026年7月31日套餐变动分析报告分析日期:2026年8月2日 数据来源:智谱官方文档 (docs.bigmodel.cn)、IT之家、新浪财经、CreditsPlan.cn、CSDN一、背景概述 2026年7月30日,智谱正式将 GLM Coding Plan 套餐改版升级&…

2026/8/3 8:57:46

口腔出现白色斑块不痛不痒,需要警惕吗

口腔出现白色斑块不痛不痒,需要警惕吗口腔黏膜上突然出现一块白色斑块,不疼也不痒,很多人刚开始都容易忽略。我身边有个同事前阵子就遇到了,他老觉得嘴里有块东西,用舌头舔感觉粗糙,但饭照吃、觉照睡&#…

2026/8/3 9:52:49

C++迭代器设计模式与高效遍历实践

1. 迭代器设计基础与核心价值在C开发中,迭代器就像图书馆的智能导航机器人。想象你面对一个巨大的书库(容器),需要逐本检查书籍(元素)。迭代器就是那个能记住当前位置、知道如何取下一本书、并能告诉你何时…

2026/8/3 9:52:49

在线打字工具哪个好用?8 款打字练习 / 测速网站深度对比

很多人练盲打、提升打字速度都会踩坑:零基础用纯测速网站练,固化二指禅错误指法;办公练中文选海外工具,素材不贴合日常场景;竞速测速选错平台,成绩虚高无参考性。市面上主流打字工具分为两类:系…

2026/8/3 9:52:49

FIFA 23生涯模式终极改造:免费开源修改器完整使用教程

FIFA 23生涯模式终极改造:免费开源修改器完整使用教程 【免费下载链接】FIFA-23-Live-Editor FIFA 23 Live Editor 项目地址: https://gitcode.com/gh_mirrors/fi/FIFA-23-Live-Editor 还在为FIFA 23生涯模式的限制感到沮丧吗?想要完全掌控你的足…

2026/8/3 9:52:49

WebAssembly SIMD加速Node.js字符串匹配实战

1. 为什么需要SIMD加速字符串匹配? 字符串匹配是计算机科学中最基础也最频繁的操作之一。从简单的文本搜索到复杂的模式识别,几乎每个应用都会涉及到字符串处理。在Node.js环境中,常见的场景包括: HTTP请求头解析 日志分析处理 …

2026/8/3 9:47:48

C++ IO流核心概念与实战应用详解

1. C IO流核心概念解析 在C编程中,IO流(Input/Output Stream)是与外部设备进行数据交互的核心机制。不同于C语言的printf/scanf函数族,C通过面向对象的方式将输入输出抽象为流对象,提供了更安全、更灵活的接口设计。流…

2026/8/2 0:02:18

如何用免费工具突破游戏窗口限制:SRWE完整使用指南

如何用免费工具突破游戏窗口限制:SRWE完整使用指南 【免费下载链接】SRWE Simple Runtime Window Editor 项目地址: https://gitcode.com/gh_mirrors/sr/SRWE 你是否遇到过这样的困扰?想为心爱的游戏截图,却发现游戏不支持自定义分辨率…

2026/8/2 1:52:02

实测才敢推 AI论文网站 2026最新测评与推荐

2026年真正好用的AI论文网站,核心看生成的论文质量、低AI味、格式正确、学术适配四大指标。综合实测,千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队,覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。一、综…

2026/8/1 0:03:49

2026必备!AI论文网站测评:最新推荐与深度对比

2026年真正好用的AI论文网站,核心看生成的论文质量、低AI味、格式正确、学术适配四大指标。综合实测,千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队,覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。 一、…

2026/8/2 8:56:50

摆脱论文困扰!盘点2026年全网爆红的的AI论文写作工具

一天写完毕业论文在2026年已不再是天方夜谭。2026年最炸裂、实测能大幅提速的AI论文写作工具,覆盖选题构思、文献整理、内容生成、格式排版等核心场景,真正帮你高效搞定论文难题。 一、全流程王者:一站式搞定论文全链路(一天定稿首…