본문 바로가기

개발/언리얼

보간(Interp)을 이용한 부드러운 액터 움직임 구현

언리얼의 수학 라이브러리를 이용하면 복잡한 계산들을 매우 간단하게 할 수 있는데,

회전과 이동에 사용할 수 있는 VInterpTo와 RInterpTo를 알아보려한다.

 

각 함수의 이름은 각각 Vector Interpolate To (Target), Rotator Interpolate To (Target)으로 이해되며

두 함수의 이용방법 또한 동일한 모습을 띄나, Vector을 사용하는지  Rotator를 이용하는지 정도의 차이가 있다.

 

static FVector VInterpTo  
(  
    const FVector & Current,  
    const FVector & Target,  
    float DeltaTime,  
    float InterpSpeed  
)
static FRotator RInterpTo  
(  
    const FRotator & Current,  
    const FRotator & Target,  
    float DeltaTime,  
    float InterpSpeed  
)

파라미터의 이름으로 알 수 있듯,
Current지점(방향)에서 Target지점(방향)을, DeltaTime시간값과 속도값(일반적으로 5~20f)을 받아

보간법을 통해 이동량 또는 회전량을 반환한다.

 

두 지점 사이에 따른 가중치가 존재하여 처음에는 빠르게, 끝에가서는 천천히 부드럽게 움직일 수 있도록 값을 제공한다.

단 만약 Speed값을 0으로 하면 목표지점값을 즉시 반환하므로 순간이동을 할 것이 아니라면 0보다 큰 수를 이용하는것이 좋다.

 

 

사용 예 (Tick()함수로부터 호출되며, TargetLocation은 사용자가 지정)

void AParantClass::Move(float DeltaTime) {
	// 액터의 현재 위치
	FVector CurrentLoc = GetActorLocation();
	// Vector-Interp-to, 벡터값 보간방식, 
	FVector NextLoc = FMath::VInterpTo(CurrentLoc, TargetLocation, DeltaTime, 0);
	SetActorLocation(NextLoc);
}

void AParantClass::Turn(float DeltaTime) {
	// 액터의 현재 회전값
	FRotator CurrentRot = GetActorRotation();
	// Rotate-Interp-to, 회전값 보간방식, 
	FRotator NextRot = FMath::RInterpTo(CurrentRot, TargetRotation, DeltaTime, 0);
	SetActorRotation(NextRot);
}

 

 

 

 

 

 

 

 

 

공식문서

https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/Core/FMath/VInterpTo

 

 

RInterp To | Unreal Engine 5.7 Documentation | Epic Developer Community

RInterp To

dev.epicgames.com

 

https://dev.epicgames.com/documentation/en-us/unreal-engine/API/Runtime/Core/FMath/RInterpTo

 

FMath::RInterpTo | Unreal Engine 5.7 Documentation | Epic Developer Community

Interpolate rotator from Current to Target.

dev.epicgames.com