虚幻四C++入坑指南02:官方案例FloatingActor详解

虚幻四C++入坑指南02:官方案例FloatingActor详解

00

我好难啊
FloatingActor的案例
1、官网一种代码(加载了Cube)
2、b站视频一种代码(加载了Cube和旋转)
3、网上的exe格式文档(什么都不加载,也没有旋转)
我也是视频都看完才意识到我手头有三种代码。

01 FloatingActor.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

//#include "CoreMinimal.h"
#include "GameFramework/Actor.h"//框架类
#include "FloatingActor.generated.h"//保证该行在最下面,想想generated的含义

UCLASS()//标识符该类能被UE4识别
class QUICKSTART_API AFloatingActor : public AActor//项目名 类名,继承自
{
	GENERATED_BODY()//宏替换

public:
	// Sets default values for this actor's properties
	// 设置此actor属性的默认值
	AFloatingActor();


	//VisibleAnyWhere,到处(一般主、BP编辑器就是到处)可见,公开到主、BP编辑器
	//宏(属性标识符)
	UPROPERTY(VisibleAnyWhere)
		//声明未赋值
		//UStaticMeshComponet,静态网络体组件,显示物体
		UStaticMeshComponet *VisualMesh;

protected:
	// Called when the game starts or when spawned
	// 当游戏开始或生成时调用
	//重写虚函数
	virtual void BeginPlay() override;

public:
	// Called every frame
	 // 在每一帧调用
	virtual void Tick(float DeltaTime) override;


	//没有的,没基础的,找了好久float RunningTime;//new,RunningTime 变量来随时间追溯移动的轨迹
};

02 FloatingActor.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "QuickStart.h"
#include "FloatingActor.h"

// Sets default values
// 设置默认值
AFloatingActor::AFloatingActor()
{
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	//逐帧调用Tick,耗性能
	// 将此actor设置为在每一帧都调用Tick()。  
	//如果您不需要这项功能,您可以关闭它以改善性能。
	PrimaryActorTick.bCanEverTick = true;
	//指针赋值
	//将指向模板类型的指针UStaticMeshComponent的指针,强转为指向现在类型的指针
	VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	//RootComponent根组件,形状小白球
	//将静态网格体组件附加到根组件
	VisualMesh->SetupAttachment(RootComponent);
	
	//静态类包含模板结构体
	//作用是到“初学者内容”文件夹下查找一个资源地址
	//Game/StarterContent/Shapes/Shape_Cube.Shape_Cube,在UE4中是有的
	//这叫资源引用,或者资源硬编码(硬加载),资源不存在会崩掉。所以要有一个if语句判断
	//一下连着的代码,StaticMesh那里加一个Cube,去掉则默认为空,在UE4中自己可以随便加
	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 AFloatingActor::BeginPlay()
{
	Super::BeginPlay();//调用父类的构造方法,调用BeginPlay之前先调用父类Actor

}

// Called every frame
// 在每一帧调用
void AFloatingActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	//以下5句,平滑地上下跳动
	//声明变量接收地址
	//获取位置
	FVector NewLocation = GetActorLocation();
	//获取旋转,FRotator写成FVector ,找了好久
	FRotator NewRotation = GetActorRotation();
	/*GetGameTimeSinceCreation的源代码
	float AActor::GetGameTimeSinceCreation() const
	{
		if (UWorld* MyWorld = GetWorld())
		{
			return MyWorld->GetTimeSeconds() - CreationTime;		
		}
		// return 0.f if GetWorld return's null
		else
		{
			return 0.f;
		}
	}
	*/
	//RunningTime 变量来随时间追溯移动的轨迹
	//获取当前物体运行时间
	float RunningTime = GetGameTimeSinceCreation();
	//每帧运行的高度DeltaHeight
	//RunningTime + DeltaTime时间Cube的高度 - RunningTime时间Cube的高度
	float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
	//sin(-1,1)*20 == -20,20,上下位移20
	NewLocation.Z += DeltaHeight * 20.0f;      //把高度以20的系数进行缩放
	float DeltaRotation = DeltaTime * 20.0f;
	RunningTime += DeltaTime;
	SetActorLocation(NewLocation);
}

03 图文讲解

在这里插入图片描述

03 01 在VS中生成后,UE4就有该类,

03 02 将该类生成蓝图类

03 03 将蓝图类拖上关卡

03 04 .h

注释掉的话,4括号里面的都会处于不可选状态

UPROPERTY(VisibleAnyWhere)

03 05 .cpp

这一段去掉的话,生成的C++类产生的BP类的“StaticMesh”就会空白,如下图,Cube会消失,剩下一个球“根组件”,播放时它不会显示的。
在这里插入图片描述

	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));
	}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值