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);

    }
}
复制代码

 

posted @   剑起苍穹  阅读(3778)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
/*鼠标点击特效*/
点击右上角即可分享
微信分享提示