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

UE_CPP学习

虚幻编辑器设置

1767186940261

1767186893108

案例1——控制物体的移动

创建一个蓝图

1767189226779

创建一个C++ Class

1767189252400

把脚本挂到蓝图中,并在脚本下面挂上一个静态的Mesh

1767189313118

进IDE编写代码

NewMovement.h

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "Components/SceneComponent.h"
#include "NewMovement.generated.h"UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class MYPROJECT_API UNewMovement : public USceneComponent
{GENERATED_BODY()public:// Sets default values for this component's propertiesUNewMovement();protected:// Called when the game startsvirtual void BeginPlay() override;public:// Called every framevirtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;// 成员变量UPROPERTY(EditAnywhere, Category = "Movement")FVector MoveOffset;  // 移动偏移UPROPERTY(EditAnywhere, Category = "Movement")float Speed = 100.0f;  // 移动速度FVector StartLocation;  // 初始位置FVector MoveOffsetNormal;   // 归一化后的移动偏移float EndDistance;      // 总移动距离float CurrentDistance = 0.0f;  // 当前移动的距离int32 DirectionMove = 1; };

NewMovement.cpp

// Fill out your copyright notice in the Description page of Project Settings.#include "NewMovement.h"// Sets default values for this component's properties
UNewMovement::UNewMovement()
{
// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;// ...
}// Called when the game starts
void UNewMovement::BeginPlay()
{
Super::BeginPlay();
StartLocation = this->GetRelativeLocation(); 
EndDistance = MoveOffset.Length();
//归一化
MoveOffset.Normalize();
MoveOffsetNormal = MoveOffset;}// Called every frame
void UNewMovement::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
//设置物体的相对位置
SetRelativeLocation(StartLocation + CurrentDistance * MoveOffsetNormal);
//更新当前的移动距离
CurrentDistance += DeltaTime * Speed * DirectionMove;
//方向反转
if (CurrentDistance >= EndDistance || CurrentDistance <= 0.0f){
DirectionMove *= -1;}
}

效果:

1767192134578

Unreal C++基础

01创建和设置Class

http://www.jsqmd.com/news/173470/

相关文章:

  • Java毕设项目:基于SpringBoot的在线招聘系统系统设计与实现(源码+文档,讲解、调试运行,定制等)
  • 基于SSM的大学生就业信息平台【源码+文档+调试】
  • 【毕业设计】基于SpringBoot的新能源二手汽车销售平台的设计与实现(源码+文档+远程调试,全bao定制等)
  • 再见,2025。你好,2026
  • 计算机Java毕设实战-基于SpringBoot的在线招聘系统系统设计与实现【完整源码+LW+部署说明+演示视频,全bao一条龙等】
  • jspm基于JSP高校研招考研招生信息共享系统的设计与实现-vue
  • 【课程设计/毕业设计】基于SpringBoot的新能源二手汽车销售平台的设计与实现【附源码、数据库、万字文档】
  • 提示工程伦理常见风险图谱:架构师必须警惕的10大伦理陷阱
  • springboot乘用车汽车销售市场销量情况可视化分析gnsm7wu6
  • Java计算机毕设之基于SpringBoot的新能源二手汽车销售平台的设计与实现(完整前后端代码+说明文档+LW,调试定制等)
  • windows系统中,GDT中的全局描述符和LDT中的局部描述符关系概述
  • 47. Connecting register env
  • java基于SSM线上视频学习系统设计与实现-vue
  • Java毕设项目:基于SpringBoot的新能源二手汽车销售平台的设计与实现(源码+文档,讲解、调试运行,定制等)
  • 48. UVM Register Backdoor Access
  • AI元人文:内在的意义行为原生——意识作为第一舞台的元叙事
  • 计算机Java毕设实战-基于SpringBoot的新能源二手汽车销售平台的设计与实现【完整源码+LW+部署说明+演示视频,全bao一条龙等】
  • java基于Ssm的线上花店鲜花销售商城的设计与实现-vue
  • 大数据时代下 Eureka 的性能优化秘籍
  • 生物智能+提示工程架构师:开启前所未有的技术新纪元
  • 2025必备!8个AI论文平台,助你轻松搞定本科毕业论文!
  • springboot大学生平时成绩量化管理系统-vue
  • springboot大学生科研项目评审系统-vue
  • 12.31.2
  • 【课程设计/毕业设计】基于java-springboot非遗作品交易平台SpringBoot的非遗传统手工艺购物系统【附源码、数据库、万字文档】
  • 贪心算法专题(十四):万流归宗——「合并区间」
  • Java计算机毕设之基于SpringBoot的非遗传统手工艺购物系统基于SpringBoot+Vue的非遗传统手工艺购物系统管理系统设计与实现(完整前后端代码+说明文档+LW,调试定制等)
  • (2025最后一篇博客)Metasploit框架攻击Windows实例:三种渗透路径
  • 贪心算法专题(十六):完美落幕的终极监控——「监控二叉树」
  • 何渡