Vector3.Slerp用于平滑转向

# 第1种写法:

Vector3.Slerp(v1, v2, percent)

using UnityEngine;

public class SmoothSetDirection : MonoBehaviour
{

    public Transform tf1;
    public Transform tf2;
    public float percentSpeed = 0.5f;
    public bool running = false;

    private float _percent;

    void Update()
    {
        if (running)
        {
            var v1 = tf1.position;
            var v2 = tf2.position;

            _percent += percentSpeed * Time.deltaTime;
            var percent = _percent;
            if (percent >= 1)
            {
                running = false;
                _percent = 0;
            }
            transform.forward = Vector3.Slerp(v1, v2, percent);
        }
    }
}

 

 

# 网上看到的另一种写法,试了下貌似有点问题

Vector3.Slerp(cur, v2, diffPercent)

unity中关于物体的旋转和朝向控制_夫人的泡泡鱼的博客-CSDN博客_unity物体朝向

using UnityEngine;

public class SmoothSetDirection : MonoBehaviour
{

    public Transform tf1;
    public Transform tf2;
    public float percentSpeed = 0.5f;
    public bool running = false;

    void OnEnable()
    {
        transform.forward = tf1.position;
    }

    void Update()
    {
        if (running)
        {
            var v1 = tf1.position;
            var v2 = tf2.position;

            var diffPercent = percentSpeed * Time.deltaTime;
            transform.forward = Vector3.Slerp(transform.forward, v2, diffPercent);
        }
    }
}

 

posted @ 2022-06-18 20:50  yanghui01  阅读(410)  评论(0)    收藏  举报