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

UEC++ 创建自定义类型案例

一,子弹类型

// .h文件 #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Components/SphereComponent.h" #include "GameFrameWork/ProjectileMovementComponent.h" #include "MyActor.generated.h" // ... UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtr<UStaticMeshComponent> Sphere; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtr<USphereComponent> SphereCollision; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtr<UProjectileMovementComponent> ProjectileMovement;
// .cpp文件 AMyActor::AMyActor() { PrimaryActorTick.bCanEverTick = true; Sphere = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Sphere")); SphereCollision = CreateDefaultSubobject<USphereComponent>(TEXT("SphereCollision")); ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovement")); static ConstructorHelpers::FObjectFinder<UStaticMesh> 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) TObjectPtr<USpringArmComponent> SpringArm; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtr<UCameraComponent> Camera; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtr<UInputMappingContext> DefaultMappingContext; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtr<UInputAction> MoveAction; UPROPERTY(EditAnywhere, BlueprintReadWrite) TObjectPtr<UInputAction> LookAction; public: void Move(const FInputActionValue& value); void Look(const FInputActionValue& value); // ...
// .cpp文件 AMyCharacter::AMyCharacter() { PrimaryActorTick.bCanEverTick = true; SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm")); Camera = CreateDefaultSubobject<UCameraComponent>(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 = Cast<APlayerController>(Controller)) if (auto Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer())) Subsystem->AddMappingContext(DefaultMappingContext, 0); } void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); if (auto EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(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.Get<FVector2D>(); 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.Get<FVector2D>(); if (Controller) { AddControllerYawInput(LookAxisVector.X); AddControllerPitchInput(LookAxisVector.Y); } }
http://www.jsqmd.com/news/1319036/

相关文章:

  • Vibe Coding实践:为Minecraft 1.8.9打造轻量级UI增强模组
  • 5分钟掌握猫抓插件:浏览器资源嗅探与媒体捕获完全手册
  • Nmap 高阶用法:存活探测、端口扫描、漏洞脚本扫描、防火墙绕过扫描参数
  • Azure Sphere与Grove套件:快速构建安全物联网原型
  • 为什么HiGHS正在重塑数学优化领域的游戏规则?
  • 一文看懂geo供应商哪家强:从技术底座到落地效果的全景对比 - 趣闻早乐评
  • WMS系统核心功能与智能仓储管理实践
  • Python性能优化工具全解析:PyPy、Cython与Numba实战
  • 嵌入式开发与MBD仿真建模:从理论到实践
  • KL散度:从信息论到机器学习实战的核心分布差异度量
  • 2026合肥婚纱摄影工作室排名前十名单 - 滚动商讯
  • Linux文件传输工具全解析:FTP、SFTP、SCP与rz/sz实战指南
  • HYDRUS1D在土壤污染物迁移模拟中的应用与实践
  • C# 学习总结:从泛型与集合到IO、多线程
  • 电商出图助手:上传产品图,7种电商风格一键生成,告别繁琐作图
  • Universal x86 Tuning Utility:全面掌控硬件性能的终极解决方案
  • Ant Design Vue 表单验证进阶:自定义 validator 函数实战指南
  • 王者荣耀阿轲实战攻略:从机制解析到节奏掌控,告别两极分化
  • interim 词义
  • Midscene虚拟屏技术与Puppeteer集成解析
  • Jetpack Compose中AnimatedVisibility动画组件详解
  • 千问 APP 新用户 8 元无门槛优惠券全攻略
  • Codex++ 安全边界探秘:当 AI 不只写代码,还能执行命令
  • Nginx的升级
  • PSO算法优化光伏MPPT技术应对局部遮阴问题
  • 三相电机核心参数测量指南:相电阻、电感与极对数的实用方法
  • Spring Profile配置详解:active与include实战技巧
  • LeetCode 1022:二叉树路径二进制求和解析与实现
  • 金华橱柜全屋定制制造商有哪些
  • DDrawCompat完整指南:如何让Windows 11完美运行经典DirectX游戏