본문 바로가기
언리얼_엔진_게임개발_공부/언리얼 C++

[언리얼/C++] Tick 함수로 메쉬 스케일 변경 (커졌다 작아졌다 하게)

by jaboy 2025. 1. 21.

1. 현재 메쉬의 스케일을 업데이트 계속해서 업데이트

2. 최솟값보다 작아지면 커지도록, 최댓값보다 커지면 작아지도록 --> 스케일 변경 속도값의 부호를 반전

/********** .h file *****************/
// 1. CurrScale : keeps track of current scale
// 2. MaxScale, MinScale : Maximum and minimum value for current scale
// 3. SpeedScale : speed at which scale changes
FVector CurrScale;
float MinScale, MaxScale, SpeedScale;

/*********** .cpp file **************/
// Variable initialized in constructor
AMyActor::AMyActor()
{
	//...
    CurrScale = FVector(2.f);
    MinScale = 1.f;
    MaxScale = 3.f;
    SpeedScale = 1.f;
}

// Set relative scale of the static mesh component, which is attached to root component
void BeginPlay()
{
	//...some code to initialize components and set up attachments...
	StaticMeshComp->SetRelativeScale3D(CurrScale);
}

// Scale gets updated and reflected in Tick:
void Tick(float DeltaTime)
{
	// Reverse direction of scale change
    if (CurrScale.Z >= MaxScale || CurrScale.Z <= MinScale)
	{
		SpeedScale *= -1;
	}
    
    if (!FMath::IsNearlyZero(SpeedScale * DeltaTime))
    {
    	// Multiply DeltaTime to keep it frame rate independent
    	CurrScale += FVector(SpeedScale * DeltaTime);
        StaticMeshComp->SetRelativeScale3D(CurrScale);
    }
}