unreal 8기

Unreal Engine C++ 활용 프로그램 제작

왕건 2026. 3. 25. 18:52

액터를 부모로 하는 C++ 생성
MyActor1을 부모로 하는 블프클 생성
스태틱 메쉬를 큐브로 설정하여 보이게 함

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


#include "MyActor1.h"

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

}

// Called when the game starts or when spawned
void AMyActor1::BeginPlay()
{
	Super::BeginPlay();
	SetActorLocation(FVector(0, 50, 0));
	SetActorRotation(FRotator(0, 0, 0));
    }

// Called every frame
void AMyActor1::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

 

BeginPlay에 SetActorLocation을 통해 액터의 시작위치를 0, 50, 0 으로 초기화

SetActorRotation을 통해 시작 각도를 0, 0, 0 으로 초기화

 

void AMyActor1::Move(int  Lx, int Ly, int Lz)
{
	SetActorLocation(FVector(Lx, Ly, Lz));
}

void AMyActor1::Turn(int Rx, int Ry, int Rz)
{
	SetActorRotation(FRotator(Rx, Ry, Rz));
}

 

Move와 Turn 함수를 만듬.

 

for (int i = 1; i <= 10; i++) 
	{
		int RandomX = FMath::RandRange(0, 100);
		int RandomY = FMath::RandRange(0, 100);
		int RandomZ = FMath::RandRange(0, 100);

		Move(RandomX, RandomY, RandomZ);

		int RandomX = FMath::RandRange(0, 100);
		int RandomY = FMath::RandRange(0, 100);
		int RandomZ = FMath::RandRange(0, 100);

		Turn(RandomX, RandomY, RandomZ);
	}

 

랜덤으로 0부터 100까지의 숫자를 받아 총 10번 이동과 회전을 하도록 만듬.

 

void AMyActor1::BeginPlay()
{
	Super::BeginPlay();

	SetActorLocation(FVector(0, 50, 0));
	GEngine->AddOnScreenDebugMessage(1, 3.f, FColor::Red, FString::Printf(TEXT("X = %.1f,Y = %.1f,Z = %.1f"), GetActorLocation().X, GetActorLocation().Y, GetActorLocation().Z));
	SetActorRotation(FRotator(0, 0, 0));

	for (int i = 1; i <= 10; i++) 
	{
		int RandomX = FMath::RandRange(0, 300);
		int RandomY = FMath::RandRange(0, 300);
		int RandomZ = FMath::RandRange(0, 300);

		Move(RandomX, RandomY, RandomZ);
		GEngine->AddOnScreenDebugMessage(1, 3.f, FColor::Red, FString::Printf(TEXT("X = %.1f,Y = %.1f,Z = %.1f"), GetActorLocation().X, GetActorLocation().Y, GetActorLocation().Z));

		int RandomLX = FMath::RandRange(0, 300);
		int RandomLY = FMath::RandRange(0, 300);
		int RandomLZ = FMath::RandRange(0, 300);

		Turn(RandomLX, RandomLY, RandomLZ);
	}
 }

 

GetActorLocation을 이용해 현재 액터의 위치 X Y Z를 받아와 왼쪽 위에 띄우게 했다.

 

하지만 너무 빨라서 시작되자마자 큐브가 이미 10번 다 움직여버려 움직임을 확인하기 힘들었다.

 

그래서 for문안에 작성된걸 따로 MoveAndTurn함수로 빼고 타이머함수가 있어 그걸로 1초에 한번 실행되도록 해결을 해보려고 했다.

void AMyActor1::MoveAndTurn()
{
	int RandomX = FMath::RandRange(0, 300);
	int RandomY = FMath::RandRange(0, 300);
	int RandomZ = FMath::RandRange(0, 300);

	Move(RandomX, RandomY, RandomZ);
	GEngine->AddOnScreenDebugMessage(1, 3.f, FColor::Red, FString::Printf(TEXT("X = %.1f,Y = %.1f,Z = %.1f"), GetActorLocation().X, GetActorLocation().Y, GetActorLocation().Z));

	int RandomLX = FMath::RandRange(0, 300);
	int RandomLY = FMath::RandRange(0, 300);
	int RandomLZ = FMath::RandRange(0, 300);

	Turn(RandomLX, RandomLY, RandomLZ);

	Count++;

	if (Count > 10)
	{
		GetWorldTimerManager().ClearTimer(MoveTimerHandle);
	}
}

 

따로 함수로 뺀 MoveAndTurn 함수이다. 안에 카운트를 세어 10번이 되면 타이머를 정지하도록 했다.

 

완성된 코드

//MyActor1.h

#pragma once

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

UCLASS()
class HELLO_API AMyActor1 : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AMyActor1();
	void Move(int Lx, int Ly, int Lz);
	void Turn(int Rx, int Ry, int Rz);
	void MoveAndTurn();
	FTimerHandle MoveTimerHandle;
	int Count = 0;
		
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;


public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};
//MyActor1.cpp


#include "MyActor1.h"

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

}

void AMyActor1::Move(int  Lx, int Ly, int Lz)
{
	SetActorLocation(FVector(Lx, Ly, Lz));
}

void AMyActor1::Turn(int Rx, int Ry, int Rz)
{
	SetActorRotation(FRotator(Rx, Ry, Rz));
}

void AMyActor1::MoveAndTurn()
{
	int RandomX = FMath::RandRange(0, 300);
	int RandomY = FMath::RandRange(0, 300);
	int RandomZ = FMath::RandRange(0, 300);

	Move(RandomX, RandomY, RandomZ);
	GEngine->AddOnScreenDebugMessage(1, 3.f, FColor::Red, FString::Printf(TEXT("X = %.1f,Y = %.1f,Z = %.1f"), GetActorLocation().X, GetActorLocation().Y, GetActorLocation().Z));

	int RandomLX = FMath::RandRange(0, 300);
	int RandomLY = FMath::RandRange(0, 300);
	int RandomLZ = FMath::RandRange(0, 300);

	Turn(RandomLX, RandomLY, RandomLZ);

	Count++;

	if (Count > 10)
	{
		GetWorldTimerManager().ClearTimer(MoveTimerHandle);
	}
}

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

	SetActorLocation(FVector(0, 50, 0));
	GEngine->AddOnScreenDebugMessage(1, 3.f, FColor::Red, FString::Printf(TEXT("X = %.1f,Y = %.1f,Z = %.1f"), GetActorLocation().X, GetActorLocation().Y, GetActorLocation().Z));
	SetActorRotation(FRotator(0, 0, 0));

	GetWorldTimerManager().SetTimer(MoveTimerHandle, this, &AMyActor1::MoveAndTurn, 1.0f, true);
 }

// Called every frame
void AMyActor1::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

화질이 구려서 잘 안보이지만 성공