* 헤더파일
#pragma once
#include "CoreMinimal.h"
#include "MovingPlatform.h"
#include "FloatingSinePlatform.generated.h"
UCLASS()
class STRIKEZONE_API AFloatingSinePlatform : public AMovingPlatform
{
GENERATED_BODY()
private:
FVector StartLocation;
float RunningTime;
public:
AFloatingSinePlatform();
virtual void MovePlatform(float DeltaTime) override;
protected:
UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category = "MovingPlatform|Settings")
FVector MovementAmplitude;
UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category = "MovingPlatform|Settings")
float MovementFrequency;
UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category = "MovingPlatform|Settings")
float PhaseOffset;
virtual void BeginPlay() override;
};
*소스파일
#include "FloatingSinePlatform.h"
AFloatingSinePlatform::AFloatingSinePlatform()
{
RunningTime = 0.0f;
MovementAmplitude = FVector(0.0f, 0.0f, 30.0f);
MovementFrequency = 0.5f;
PhaseOffset = 0.0f;
}
void AFloatingSinePlatform::MovePlatform(float DeltaTime)
{
RunningTime += DeltaTime;
FVector NewLocation = StartLocation;
NewLocation.Z += FMath::Sin((RunningTime * MovementFrequency * 2 * PI) + PhaseOffset) * MovementAmplitude.Z;
SetActorLocation(NewLocation);
}
void AFloatingSinePlatform::BeginPlay()
{
Super::BeginPlay();
StartLocation = GetActorLocation();
if (PhaseOffset == 0.0f)
{
PhaseOffset = FMath::FRandRange(0.0f, 2 * PI);
}
}
NewLocation.Z += FMath::Sin((RunningTime * MovementFrequency * 2 * PI) + PhaseOffset) * MovementAmplitude.Z;
- RunningTime은 프레임마다 누적된 시간 값을 의미
- MovementFrequency는 움직임의 속도를 의미, Sin 함수의 반복 속도를 조절하는 역할을 한다. 기본적으로 Sin 함수는 1초에 1번 주기를 완료하는데, Frequency를 조절해 주기가 더 빨라지거나 느려지게 할 수 있다. 현재는 0.5의 값을 가지고 있으므로 2초에 한 번 주기를 완료한다.
- 2 * PI 는 원의 주기를 의미, Sin 함수는 주기적으로 반복되는 함수이며 한 주기 동안 0 → 1 → 0 → -1 → 0을 완료한다.
이 표현식은 원을 시간에 따라 그리면서, 단위원 위의 y 좌표를 가져오는 것과 같다.
시간이 흐르면서 사인 함수는 계속 주기를 반복하며, 플랫폼이 상하로 부드럽게 움직이는 것을 연출하였다.
'내배캠 > Unreal Engine' 카테고리의 다른 글
UPrimitiveComponent (0) | 2025.01.23 |
---|---|
Animation Blueprint로 캐릭터 애니메이션 구현 (0) | 2025.01.23 |
Enhanced Input System (0) | 2025.01.22 |
StaticClass( ) 메서드 (0) | 2025.01.22 |
부동 소수점 연산 오차 (0) | 2025.01.21 |