Machineboy空

코루틴 없이 Lerp로 n초 동안 scale, position 변화 본문

Game/Unity

코루틴 없이 Lerp로 n초 동안 scale, position 변화

안녕도라 2023. 9. 11. 17:20

 

public Transform drone;
Vector3 droneOriginScale;
Vector3 droneOriginPosition;

void Start()
{
	droneOriginScale = drone.localScale;
    droneOriginPosition = drone.localPosition;
}

void Update()
{
	//1초 동안
    currentTime += Time.deltaTime;
    
    if(currentTime<=1)
	{
    	//n초동안 변화하는 코드
    	Vector3 targetScale = droneOriginScale * 1.2f;
        drone.localScale = Vector3.Lerp(droneOriginScale, targetScale, currentTime);
        
        //n초 동안 진동하는 코드
        drone.localPosition = droneOriginLocalPosition + new Vector3(1,1,0) * UnityEngine.Random.value * 0.1f * currentTime;      
    }else
    {
    	//1초 후에 터지고 싶다.
        Destroy(gameObject);
    }
}

https://tech-interview.tistory.com/258

 

 

[Unity] Mathf.Lerp() 함수

Lerp 1. 개념 선형 보간(Linear Interpolation)은 두 점(a, b) 사이의 값(c)을 구하기 위해 두 점을 연결한 직선을 만들어 사이 값(t)을 계산하는 방법이다. Mathf.Lerp(float a, float b, float t) 여기서 t는 percet(0 ~ 1)

tech-interview.tistory.com

 

Vector3.Lerp(A,B,t)일 때, 

t = currentTime / n(초)로 구성하면, 

currentT N t 점 idx 점의 위치
1 10 1/10 t(1) 1/10 * 1
2 10 2/10 t(2) 2/10 *(1-t(1))
3 10 3/10 t(3) 3/10 *(1-t(2))
4 10 4/10 t(4) 4/10 *(1-t(3))
5 10 5/10 t(5) 5/10 *(1-t(4))
6 10 6/10 t(6) 6/10 *(1-t(5))
7 10 7/10 t(7) 7/10 *(1-t(6))
8 10 8/10 t(8) 8/10 *(1-t(7))
9 10 9/10 t(9) 9/10 *(1-t(8))
10 10 10/10 t(10) 10/10 *(1-t(9))

ex) 30초 => 30초 구간을 100분위로 나눈 구간을 기준으로 움직일 것

 

t는 1로 수렴하는 채워야할 값이고 그 구간을 초 시간단위로 나눠주는 느낌.

1을 다 채우기 위해서 (=수렴하기 위해서) 필요한 시간만큼으로 나눠주는 것