Quaternion.Lerp()

作者:追风剑情 发布于:2017-4-14 18:35 分类:Unity3d

示例一:通过四元数插值,使物体缓慢旋转。


using System.Collections;
using UnityEngine;

public class Test2 : MonoBehaviour {

    public float speed = 0.2f;//旋转速度

    [SerializeField]
    private Rigidbody m_Rigidbody;
    private Quaternion targetAngle;
    private float angleLerp;

    void Awake()
    {
        if (null == m_Rigidbody)
            m_Rigidbody = this.GetComponent<Rigidbody>();
    }

	void Start () {
        targetAngle = Quaternion.AngleAxis(60f, Vector3.up);
	}

    void Update()
    {
        angleLerp += speed * Time.deltaTime;
        Quaternion q = Quaternion.Lerp(Quaternion.identity, targetAngle, angleLerp);
        m_Rigidbody.MoveRotation(q);
    }
}


运行测试

22222.gif

//创建一个旋转到forward方位的四元素
Vector3 forward = transform.forward;
Quaternion quaternion = Quaternion.identity;
quaternion.SetLookRotation(forward);

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号