Unity平滑旋轉Quaternion. Slerp對象旋轉到固定角度 平滑移動 Vector3.MoveTowards


代碼如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Unity 物體旋轉到固定角度
/// </summary>
public class Rotaion : MonoBehaviour
{
    Quaternion targetPoint;
    public  Transform targetTransform;
    // Start is called before the first frame update
    void Start()
    {
        //將目標角度轉成四元素進行計算
        targetPoint = Quaternion.Euler(0, 180, 0);
    }

    // Update is called once per frame
    void Update()
    {
        //yong slerp 進行插值平滑旋轉
        transform.rotation = Quaternion.Slerp(transform.rotation, targetPoint, 2 * Time.deltaTime);
        //平滑移動   targetTransform.position  目標位置
        transform.position = Vector3.MoveTowards(transform.position, targetTransform.position, 2 * Time.deltaTime);
       
        //if (Quaternion.Angle(targetPoint,transform.rotation )<1)
        //{
        //    transform.rotation = targetPoint;
        //}
        //給旋轉直接賦值  沒有平滑旋轉 如下
        ////this.transform.localEulerAngles = new Vector3(0, 90, 0);
        ////this.transform.rotation = Quaternion.Euler(0, 90, 0);

    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM