当前位置: 首页 > 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.gsyq.cn/news/189345.html

相关文章:

  • 基于SSM的大学生就业信息平台【源码+文档+调试】
  • 计算机Java毕设实战-基于SpringBoot的在线招聘系统系统设计与实现【完整源码+LW+部署说明+演示视频,全bao一条龙等】
  • windows系统中,GDT中的全局描述符和LDT中的局部描述符关系概述
  • Java计算机毕设之基于SpringBoot的新能源二手汽车销售平台的设计与实现(完整前后端代码+说明文档+LW,调试定制等)
  • 47. Connecting register env
  • java基于SSM线上视频学习系统设计与实现-vue
  • Java毕设项目:基于SpringBoot的新能源二手汽车销售平台的设计与实现(源码+文档,讲解、调试运行,定制等)
  • AI元人文:内在的意义行为原生——意识作为第一舞台的元叙事
  • 提示工程伦理常见风险图谱:架构师必须警惕的10大伦理陷阱
  • springboot乘用车汽车销售市场销量情况可视化分析gnsm7wu6
  • springboot大学生科研项目评审系统-vue
  • 12.31.2
  • 【课程设计/毕业设计】基于java-springboot非遗作品交易平台SpringBoot的非遗传统手工艺购物系统【附源码、数据库、万字文档】
  • 何渡
  • 贪心算法专题(十六):完美落幕的终极监控——「监控二叉树」
  • 也许是,年度总结?
  • 贪心算法专题(十五):借位与填充的智慧——「单调递增的数字」
  • Java毕设选题推荐:基于SpringBoot的非遗传统手工艺购物系统基于Spring Boot的非物质文化商城系统的设计与开发【附源码、mysql、文档、调试+代码讲解+全bao等】
  • GDP-6-叠氮岩藻糖二钠盐—糖基化研究与治疗开发的创新工具 944560-34-3
  • HPE SPP 2025.11.00.00 - HPE 服务器固件
  • AI在智能家居设备能源管理与用户行为分析中的应用
  • 《韩非子》之简结
  • 软件界面开发icon下载
  • Flink JDBC Driver把 Flink SQL Gateway 变成“数据库”,让 BI / 工具 / 应用直接用 JDBC 跑 Flink SQL
  • 2025年度总结乐观版
  • 贪心算法专题(十三):画地为牢的艺术——「划分字母区间」
  • 10. 图像的形态学操作
  • 国内差旅平台综合排名全景解析:2026行业发展趋势与核心能力展望
  • Java计算机毕设之基于springboot的美食信息推荐系统的设计与实现 -基于springboot的美食网站设计与实现(完整前后端代码+说明文档+LW,调试定制等)
  • 《韩非子》之法