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

UE学习C++(1)创建actor

创建新C++类

在 虚幻编辑器 中,点击 文件(File) 下拉菜单,然后选择 新建C++类...(New C++ Class...) 命令:

此时将显示 选择父类(Choose Parent Class) 菜单。可以选择要扩展的现有类,将其功能添加到自己的类。选择 Actor,因为其是可在场景中放置的最基本对象类型,然后点击 下一步(Next)

在 为新Actor命名(Name Your New Actor) 菜单中,将Actor命名为 FloatingActor,然后点击 创建类(Create Class)

命名新类

编辑C++类

现在我们已创建C++类,将切换到Visual Studio并编辑代码。

在 Visual Studio 中,找到默认情况下显示在窗口左侧的 解决方案浏览器,然后用其找到 FloatingActor.h。在项目中,它将位于 Games> QuickStart > Source > QuickStart 下

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "create_actor.generated.h"

UCLASS()
class SPAWNDESTROY_API Acreate_actor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	Acreate_actor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
public:
	// 静态网格体成员
	UPROPERTY(VisibleAnywhere)
	UStaticMeshComponent* visualmesh;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "FloatingActor")
	float FloatSpeed = 20.0f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "FloatingActor")
	float RotationSpeed = 20.0f;

};
#include "create_actor.h"

// Sets default values
Acreate_actor::Acreate_actor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	// UStaticMeshComponent 类型的默认子对象。这个组件将用于处理该 Actor 的静态网格模型。
	visualmesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	// 将 visualmesh 组件附加到 Actor 的根组件(RootComponent)上,这意味着它将与该 Actor 一起移动、旋转或缩放。
	visualmesh->SetupAttachment(RootComponent);
	// FObjectFinder 类在游戏资源中查找一个名为 "Shape_Cube" 的静态网格模型资源(UStaticMesh)
	static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));

	if (CubeVisualAsset.Succeeded())
	{
		// 绑定对应的资源
		visualmesh->SetStaticMesh(CubeVisualAsset.Object);
		// 设置相对资源
		visualmesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
	}


}

// Called when the game starts or when spawned
void Acreate_actor::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void Acreate_actor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	FVector NewLocation = GetActorLocation();
	FRotator NewRotation = GetActorRotation();
	float RunningTime = GetGameTimeSinceCreation();
	float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
	NewLocation.Z += DeltaHeight * FloatSpeed;          //按FloatSpeed调整高度
	float DeltaRotation = DeltaTime * RotationSpeed;    //每秒旋转等于RotationSpeed的角度
	
	SetActorLocationAndRotation(NewLocation, NewRotation);

}


http://www.kler.cn/news/156118.html

相关文章:

  • notepad++ 插件JSONView安装
  • 大数据技术学习笔记(七)—— Zookeeper
  • Leetcode—1423.可获得的最大点数【中等】
  • solidity实现ERC20代币标准
  • MySQL数据库,初学SQL知识点引入
  • Elk+Filebeat+Kafka实现日志收集
  • Pandas进阶:拼接 concat 使用方法
  • 【EasyExcel实践】万能导出,一个接口导出多张表以及任意字段(可指定字段顺序)
  • Kubernetes1.27容器化部署Prometheus
  • YoloV8改进策略:Swift Parameter-free Attention,无参注意力机制,超分模型的完美迁移
  • 2024美赛数学建模资料---100%获奖资料
  • Selenium 学习(0.17)——软件测试之测试用例设计方法——白盒测试——逻辑覆盖法(条件覆盖和条件判定覆盖)
  • openGauss学习笔记-140 openGauss 数据库运维-例行维护-例行维护表
  • java学习part27线程死锁
  • 数据库-PostgreSQL学习笔记
  • Presto:基于内存的OLAP查询引擎
  • C/C++ 整数二分以及浮点数二分
  • 项目实战一-性能测试筑基
  • 【PTA-C语言】编程练习3 - 循环结构Ⅰ
  • 蓝桥杯物联网竞赛_STM32L071_8_ADC扩展模块
  • 什么是Anaconda
  • jsp高校教师调课管理系统Myeclipse开发mysql数据库web结构java编程计算机网页项目
  • GeoServer漏洞(CVE-2023-25157)
  • 491. 递增子序列
  • Golang语言基础之切片
  • 一些后端测试的东西
  • day5 两数之和为x
  • SSM SpringBoot vue社团事务管理系统
  • mysql中的锁及其作用
  • 【迅搜05】索引配置(二)字段定义与设计