UE4基础:快速开始实例(Actor基础操作)

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

#pragma once

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

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

	//使其在编辑器中可见
	//UPROPERTY(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;

};

在这里插入图片描述
如果加上
UPROPERTY(VisibleAnywhere)
即在虚幻编辑器中可见
在这里插入图片描述

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


#include "CubeActor.h"

// Sets default values
ACubeActor::ACubeActor()
{
 	// 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;



	//VisualMesh引用填充新的静态网格组件,并将其附加到Actor上
	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 ACubeActor::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
//每一帧运行一次
void ACubeActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	//获取位置
	FVector NewLocation = GetActorLocation();
	//获取角度
	FRotator NewRotator = GetActorRotation();
	//获取创建时间
	float RunningTime = GetGameTimeSinceCreation();
	//设置高度基准,sinx函数是周期函数。
	float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));

	NewLocation.Z += DeltaHeight * 20.0f;
	//设置位置
	SetActorLocationAndRotation(NewLocation, NewRotator);

}


}


游戏运行:
UCLASS()可以使虚幻引擎识别类
GENERATED_BODY()加载前置条件
类初始化,构造函数ACubeActor::ACubeActor()运行,
调用父类BeginPlay()
调用Tick(float DeltaTime)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xMathematics

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值