官方示例代码FloatingActor

参考链接

QuickStart

FloatingActor.h

#define FloatingActor.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"//一些核心的东西,比如一些数据类型
#include "GameFramework/Actor.h"//Actor父类
#include "FloatingActor.generated.h"//与ue4反射机制有关,必须放在所有头文件下方

UCLASS()//可以理解为类的标识符,标识下面这是一个类
//QUICKSTART_API 可以理解为一个命名空间,AFloatingActor才是类名,继承自AActor
class QUICKSTART_API AFloatingActor : public AActor
{
	GENERATED_BODY()//这个是宏,用来支持反射、序列化等C++本来没有的机制
	
public:	
	// Sets default values for this actor's properties
	//默认构造函数
	AFloatingActor();
	UPROPERTY(VisibleAnywhere)//将变量公开到编辑器或者蓝图,VisibleAnywhere修饰符设置该属性为任何地方都可见,是一个标识符
	//如果注释掉这句话,那么在编辑器中,就无法对这个变量的位置、缩放等进行设置
	UStaticMeshComponent* VisualMesh;//静态网格体组件的指针,在此还未初始化

protected:
	// Called when the game starts or when spawned
	//游戏一开始就调用的
	virtual void BeginPlay() override;

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

};

FloatingActor.cpp

#define FloatingActor.cpp
// Fill out your copyright notice in the Description page of Project Settings.


#include "FloatingActor.h"

// Sets default values
AFloatingActor::AFloatingActor()
{
	/*
	构造函数,它在类首次创建时告诉该类如何初始化自身。
	我们添加的代码将会在VisualMesh引用中填充新的StaticMeshComponent、 将它附加到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;//将Actor设置为逐帧调用Tick(),不需要则设为false
	VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));//创建一个静态网格体,并且赋值地址给指针
	VisualMesh->SetupAttachment(RootComponent);//把静态网格体附加到根组件

	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();
	
}

// Called every frame
void AFloatingActor::Tick(float DeltaTime)
{
	/*
	实时执行的代码。在此例中,它将使立方体在上下浮动的同时旋转。
	*/
	//调用父类方法
	Super::Tick(DeltaTime);
	
	//获取当前物体的位置
	FVector NewLocation = GetActorLocation();
	//获取物体的旋转
	FRotator NewRotation = GetActorRotation();
	//获取物体当前运行时间
	float RunningTime = GetGameTimeSinceCreation();
	//物体每帧高度,让物体的高度跟时间挂钩,就像sin函数图像那样,非常巧妙
	float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
	//把高度以20的系数进行缩放
	NewLocation.Z += DeltaHeight * 20.0f;       //Scale our height by a factor of 20
	float DeltaRotation = DeltaTime * 20.0f;    //Rotate by 20 degrees per second
	NewRotation.Yaw += DeltaRotation;
	//设置位置和旋转
	SetActorLocationAndRotation(NewLocation, NewRotation);
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值