All Development

Unity) Lerp 의 사용법

Todah 2023. 10. 28. 21:24
반응형

Lerp 란?

 유니티에서 제공하는 선형보간법으로 두 점 사이의 거리를 선형으로 반환해준다. 보통 특정시간내에 특정 위치로 이동하거나 특정 값으로 바꾸는 작업을 할때 사용한다. Lerp 함수는 시작 값, 끝 값 그리고 보간을 어느 정도까지 해야하는 지를 나타내는 t값(0과 1사이의 값)으로 구성되어 있다.

 

 

- Simple Lerp

transform.position = Vector3.Lerp(startPos, endPos, Time.deltaTime);

 Lerp를 위와 같이 사용한다면, 계속해서 startPos가 바뀌기 때문에 절대 endPos에 도착할 수 없다. 또한 t값을 바꾸고 싶을 때마다 스크립트를 수정해야하는 번거로움이 생긴다. 이러한 문제점을 개선하기 위해서는 t값을 변형할 필요가 있다.

 

 

- Change Lerp

void Lerp()
  {
    if (timeElapsed < lerpDuration)
    {
      valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
      timeElapsed += Time.deltaTime;
    }
    else 
    {
      valueToLerp = endValue;
    }
  }

 위와 같이 코드를 작성하면 timeElapsed와 lerpDuration이 같아지는 구간 생기므로, endValue에 도달할 수 있게 된다.

 

 

#참조

https://chicounity3d.wordpress.com/2014/05/23/how-to-lerp-like-a-pro/

 

How to Lerp like a pro

I see this sort of thing far too often: transform.position = Vector3.Lerp(startPos, endPos, Time.deltaTime); The person posting it is usually convinced that Vector3.Lerp is “broken”, bu…

chicounity3d.wordpress.com

https://gamedevbeginner.com/the-right-way-to-lerp-in-unity-with-examples/#how_to_use_lerp_in_unity

 

The right way to Lerp in Unity (with examples) - Game Dev Beginner

Learn to animate buttons, move objects and fade anything - the right way - in my in-depth guide to using LERP in Unity (including copy / paste examples).

gamedevbeginner.com

 

반응형

'All Development' 카테고리의 다른 글

C++) C와 C++ 의 차이점 Part 2  (1) 2024.01.05
C++) C와 C++ 의 차이점 Part 1  (2) 2024.01.04
Sparta_algorithm_course_05) velog  (1) 2022.07.29
Sparta_algorithm_course_04) velog  (1) 2022.07.23
Sparta_algorithm_course_03) velog  (0) 2022.07.14