Sin() 함수를 활용한 반복 이동#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Star.generated.h"
UCLASS()
class CH3CPP_API AStar : public AActor
{
GENERATED_BODY()
public:
AStar();
protected:
virtual void BeginPlay() override;
virtual void Tick(float DeltaTime) override;
private:
FVector StartLocation; // 초기 위치
float RunningTime; // 누적 시간
float Speed = 1.0f; // 이동 속도
float MaxHeight = 100.0f; // 최대 높이
};
#include "Star.h"
AStar::AStar()
{
PrimaryActorTick.bCanEverTick = true;
}
void AStar::BeginPlay()
{
Super::BeginPlay();
StartLocation = GetActorLocation(); // 초기 위치 저장
}
void AStar::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// 시간 누적
RunningTime += DeltaTime;
// Sin 함수를 이용한 높이 계산
float DeltaHeight = FMath::Sin(RunningTime * Speed) * MaxHeight;
// 새로운 위치 계산
FVector NewLocation = StartLocation;
NewLocation.Z += DeltaHeight;
// 위치 설정
SetActorLocation(NewLocation);
}
FMath::Sin()을 활용해서 반복적으로 변화하는 값을 계산한다.
RunningTime에 따라 액터가 부드럽게 위아래로 이동한다.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Star.generated.h"
UCLASS()
class CH3CPP_API AStar : public AActor
{
GENERATED_BODY()
public:
AStar();
protected:
virtual void BeginPlay() override;
virtual void Tick(float DeltaTime) override;
private:
FVector StartLocation; // 초기 위치
bool bIsMovingUp; // 현재 이동 방향 (위/아래)
float Speed = 50.0f; // 이동 속도
float MaxHeight = 200.0f; // 최대 높이
};
#include "Star.h"
AStar::AStar()
{
PrimaryActorTick.bCanEverTick = true;
bIsMovingUp = true; // 초기 방향은 위로 이동
}
void AStar::BeginPlay()
{
Super::BeginPlay();
StartLocation = GetActorLocation(); // 초기 위치 저장
}
void AStar::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FVector CurrentLocation = GetActorLocation();
float DeltaHeight = Speed * DeltaTime;
if (bIsMovingUp)
{
CurrentLocation.Z += DeltaHeight;
// 최대 높이에 도달하면 방향 전환
if (CurrentLocation.Z >= StartLocation.Z + MaxHeight)
{
CurrentLocation.Z = StartLocation.Z + MaxHeight;
bIsMovingUp = false;
}
}
else
{
CurrentLocation.Z -= DeltaHeight;
// 최소 높이에 도달하면 방향 전환
if (CurrentLocation.Z <= StartLocation.Z)
{
CurrentLocation.Z = StartLocation.Z;
bIsMovingUp = true;
}
}
// 새로운 위치 설정
SetActorLocation(CurrentLocation);
}
bIsMovingUp 변수로 현재 이동 방향을 제어한다.
최대 높이 또는 최소 높이에 도달하면 이동 방향을 반전한다.
AddActorWorldOffset를 활용한 이동#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Star.generated.h"
UCLASS()
class CH3CPP_API AStar : public AActor
{
GENERATED_BODY()
public:
AStar();
protected:
virtual void BeginPlay() override;
virtual void Tick(float DeltaTime) override;
private:
FVector MovementDirection; // 이동 방향
FVector StartLocation; // 초기 위치
float MovementSpeed = 200.0f; // 이동 속도
float MaxDistance = 500.0f; // 최대 이동 거리
};
#include "Star.h"
AStar::AStar()
{
PrimaryActorTick.bCanEverTick = true;
MovementDirection = FVector(1.0f, 0.0f, 0.0f); // 초기 이동 방향 (X축 방향)
}
void AStar::BeginPlay()
{
Super::BeginPlay();
StartLocation = GetActorLocation(); // 초기 위치 저장
}
void AStar::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// 이동 거리 계산
FVector DeltaLocation = MovementDirection * MovementSpeed * DeltaTime;
AddActorWorldOffset(DeltaLocation);
// 현재 위치 확인
FVector CurrentLocation = GetActorLocation();
// 최대 거리 도달 시 방향 전환
if (FMath::Abs(CurrentLocation.X - StartLocation.X) > MaxDistance)
{
MovementDirection *= -1.0f; // 방향 반전
}
}
AddActorWorldOffset를 사용하여 X축으로 이동한다.MaxDistance를 초과하면 이동 방향을 반전시킨다.| 구현 방법 | 장점 | 단점 | 활용 예시 |
|---|---|---|---|
Sin() 함수 활용 | 부드러운 주기적 움직임 구현 가능. | 일정한 패턴만 구현 가능. | 부유하는 오브젝트, 아이템 |
| 선형 이동 | 간단하고 예측 가능한 움직임 구현 가능. | 복잡한 이동 패턴은 어렵다. | 엘리베이터, 상하로 움직이는 플랫폼 |
AddActorWorldOffset 활용 | 3D 공간에서 다양한 방향으로 자유로운 이동 구현 가능. | 직접 이동 방향과 범위를 관리해야 함. | NPC 순찰 경로, 수평 이동 장애물 |