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