본문 바로가기

유니티/이론

Unity에서 Lerp

Lerp(선형 보간)

두 점 사이에 임의의 직선 위에 특정한 지점을 해당 직선 거리에 비례하여 구하는 방법이다.


 

Note.

개념은 어렵지만 일반적으로 이해하기로는 부드러운 움직임을 연출할 때 사용하는 기능인 것 같다.

 

EX.

다음 예제는 Vector3.Lerp를 이용하여 부드러운 움직임을 구현할 것이다.

Lerp의 과정을 가장 확실하게 볼 수 있어 Vector3.Lerp를 선택했다.

Transfrom을 Translate로 바꾸는 코드와 Lerp를 사용하여 바꾸는 코드를 비교하려고 한다.

 

 

Code.Lerp

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestVector3Lerp : MonoBehaviour
{
    [SerializeField]
    private float lerpValue = 0.01f;

    private Vector3 newPos;

    private void Start()
    {
        newPos = new Vector3(-8f, 0, 0);
    }

    private void Update()
    {
        transform.position = Vector3.Lerp(transform.position, newPos, lerpValue);
    }
}

오브젝트가 점점 newPos에 가까워지는 코드

 


PlayVideo.Lerp

동영상 서비스가 종료되어 해당 콘텐츠를 재생할 수 없습니다.


Code.Translate

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestVector3Lerp : MonoBehaviour
{
    [SerializeField]
    private float Speed = 10f;

    private Vector3 Direction;
    private Vector3 newPos;

    private void Start()
    {
        Direction = new Vector3(-1f, 0, 0);
        newPos = new Vector3(-8f,0,0);
    }

    private void Update()
    {
        if (transform.position.x > newPos.x)
        transform.Translate(Direction * Speed * Time.deltaTime);
    }
}

 

 

PlayVideo.Translate

동영상 서비스가 종료되어 해당 콘텐츠를 재생할 수 없습니다.

 

틀린 내용이 있다면 알려주시면 감사하겠습니다.

'유니티 > 이론' 카테고리의 다른 글

유니티 Time 클래스  (0) 2025.01.02
URP(universal/Rendering PipeLine)에 대하여  (1) 2024.08.13
2024/07/02 (화) DontDestroyOnLoad에 대해  (0) 2024.07.02
2024.06.05 코루틴이란?  (0) 2024.06.07
2024.06.03(월) Life Cycle, 유니티  (0) 2024.06.07