EasyCastleUNITY

Lerp and Slerp 본문

개인 필기

Lerp and Slerp

EasyCastleT 2023. 8. 18. 14:34

Lerp (Linear Interpolation) -> 선형보간

언제어디서든 균일한 속도로 작동

Slerp(Spherical Linear Interpolation) -> 구면 선형 보간

구를 보간

구면 선형 보간은 주로 회전로직에 사용된다.

시작지점과 종료지점은 느리게 동작한다. 

 

Test Code

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

public class LerpSlerpMain : MonoBehaviour
{
    [SerializeField] private Transform startTrans;
    [SerializeField] private Transform endTrans;
    [SerializeField] private Transform playerTrans1;
    [SerializeField] private Transform playerTrans2;
    [Range(0.0f,1.0f)] [SerializeField] private float time1;
    [Range(0.0f,1.0f)] [SerializeField] private float time2;

    private float elapsedTime = 0.0f;
    // Update is called once per frame
    void Update()
    {

        //보간된 위치
        Vector3 pos1 = Vector3.Lerp(this.startTrans.position, this.endTrans.position, this.time1);
        //Vector3 pos2 = Vector3.Slerp(this.startTrans.position, this.endTrans.position, time2); //(시작 좌표, 종료 좌표, 시간)
        
        if(this.time1 == 0.0f)
        {
            this.playerTrans1.LookAt(this.endTrans.position);
        }
        else if (this.time1 == 1.0f)
        {
            this.playerTrans1.LookAt(this.startTrans.position);
        }
        this.playerTrans1.position = pos1;
        //this.playerTrans2.position = pos2;
        //Debug.LogFormat("player's position {0}, Time:{1}",this.playerTrans.position,this.time);
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawLine(this.startTrans.position,this.endTrans.position);
    }
}

선형보간, 구면 선형보간 비교

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

public class LerpSlerpMain : MonoBehaviour
{
    [SerializeField] private Transform startTrans1;
    [SerializeField] private Transform endTrans1;

    [SerializeField] private Transform startTrans2;
    [SerializeField] private Transform endTrans2;

    [SerializeField] private Transform playerTrans1;
    [SerializeField] private Transform playerTrans2;
    [Range(0.0f,1.0f)] [SerializeField] private float time1;
    [Range(0.0f,1.0f)] [SerializeField] private float time2;

    private float elapsedTime = 0.0f;
    // Update is called once per frame
    void Update()
    {
        this.playerTrans1.position = Vector3.Lerp(this.playerTrans1.position, this.endTrans1.position,Time.deltaTime);
        this.playerTrans2.position = Vector3.Slerp(this.playerTrans2.position, this.endTrans2.position,Time.deltaTime);
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawLine(this.startTrans1.position,this.endTrans1.position);
        Gizmos.color = Color.red;
        Gizmos.DrawLine(this.startTrans2.position,this.endTrans2.position);
    }
}

두 보간 차이 비교

'개인 필기' 카테고리의 다른 글

2023/08/21 필기  (0) 2023.08.21
ref 키워드 and Vector3.SmoothDamp  (0) 2023.08.18
정규화 벡터  (0) 2023.08.18
유니티 라이프 사이클  (0) 2023.08.16
[과제] 스레드와 코루틴  (0) 2023.08.15