値を滑らかに変更する関数

Mathf

Mathf.MoveTowards(float current, float target, float maxDelta)

current現在の値
target移動先となる値
maxDelta値に適用される最大の変更

等速で動きます。

    [SerializeField] private Transform targetObj;
    [SerializeField] private Transform destination;

    private bool isMove;
    private float speed = 1f;


    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isMove = true;
        }

        if (isMove)
        {
            Vector3 curPos = targetObj.position;
            curPos.x = Mathf.MoveTowards(curPos.x, destination.position.x, Time.deltaTime * speed);

            targetObj.position = curPos;
        }
    }

Mathf.Lerp(float a, float b, float t)

a開始値
b終了値
t 2つのfloatの補間値

徐々に早くなり、徐々に遅くなるといった動きができます。

    [SerializeField] private Transform targetObj;
    [SerializeField] private Transform destination;

    private bool isMove;
    private float speed = 1f;


    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isMove = true;
        }

        if (isMove)
        {
            Vector3 curPos = targetObj.position;
            curPos.x = Mathf.Lerp(curPos.x, destination.position.x, Time.deltaTime * speed);

            targetObj.position = curPos;
        }
    }

Mathf.SmoothStep(float from, float to, float t)

from 初期値
to目的の値
t目的地点(0~1の値。0.5なら真ん中)
    [SerializeField] private Transform target;

    private bool isMove;
    private float t;


    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isMove = true;
        }

        if (isMove)
        {
            t += Time.deltaTime;
        }

        Vector3 pos = target.position;
        pos.x = Mathf.SmoothStep(-3f, 3f, t);
        target.position = pos;
    }

Mathf.SmoothDamp(float current, float target, float currentVelocity, float smoothTime)

current移動元
target移動先
currentVelocity現在の速度が格納される
smoothTime移動先へ到達するおおよその時間
    [SerializeField] private Transform targetObj;

    private bool isMove;
    private float currentVelocity;


    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isMove = true;
        }

        Vector3 pos = targetObj.position;
        float current = pos.x;
        float target = 3f;
        float smoothTime = 1f;

        if (isMove)
        {
            pos.x = Mathf.SmoothDamp(pos.x, target, ref currentVelocity, smoothTime);
            targetObj.position = pos;
        }

        Debug.Log("現在の速度:" + currentVelocity);

Vector3

Vector3.MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta)

current現在位置
target目的地
maxDistanceDelta進む距離
    [SerializeField] private Transform targetObj;
    [SerializeField] private Transform destination;

    private bool isMove;
    private float speed = 1f;


    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isMove = true;
        }

        if (isMove)
        {
            targetObj.position = Vector3.MoveTowards(targetObj.position, destination.position, Time.deltaTime * speed);
        }
    }

Vector3.Lerp(Vector3 a, Vector3 b, float t)

a現在地
b目標値
t2点間の距離を1とした場合の値
    [SerializeField] private Transform targetObj;
    [SerializeField] private Transform destination;

    private bool isMove;
    private float speed = 1f;


    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isMove = true;
        }

        if (isMove)
        {
            targetObj.position = Vector3.Lerp(targetObj.position, destination.position, Time.deltaTime * speed);
        }
    }