美文网首页
物体的几种旋转方式

物体的几种旋转方式

作者: 烂醉花间dlitf | 来源:发表于2020-10-27 14:26 被阅读0次

一种错误的写法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotation : MonoBehaviour
{
    void Update()
    {
        Vector3 euler_rotation = transform.rotation.eulerAngles;
        euler_rotation += new Vector3(20,20,0) * Time.deltaTime;
        transform.rotation = Quaternion.Euler(euler_rotation);
    }
}

错误写法的运行结果

可以看到物体只有一开始在 X 轴上是有旋转的,后面就 “卡住了”,具体的原因可以在学习了四元数与欧拉角的转换之后再探究,现在只需要知道如果想要使用欧拉角旋转,需要将整个旋转角度存在一个变量里面,不能直接从四元数获取。具体写法参考如下:

控制欧拉角的写法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotation : MonoBehaviour
{
    private Vector3 rotation;
    private Vector3 EulerRotation
    {
        get { return rotation; }
        set {
            Vector3 temp = value;
            if (temp.x > 360)
            {
                temp.x -= 360;
            }
            if (temp.y > 360)
            {
                temp.y -= 360;
            }
            if(temp.z > 360)
            {
                temp.z -= 360;
            }
            rotation = temp;
            }
    }
    // Start is called before the first frame update
    void Start()
    {
        EulerRotation = transform.rotation.eulerAngles;
    }

    // Update is called once per frame
    void Update()
    {
        EulerRotation += new Vector3(20,20,0) * Time.deltaTime;
        transform.rotation = Quaternion.Euler(EulerRotation);
    }
}

可以很直观的从欧拉角知道旋转的效果。这里其实就是将目标欧拉角一直存储在一个单独的 Vertor3 里面。
在这个过程中物体在 Inspector 面板的旋转会基本跟预期的一样,也就是说如果设置的旋转是 Vector3(20,20,0) * Time.deltaTime ,那么它在 Z 轴的旋转就不会变。看图可以看到一开始 x 和 y 都是同步增长的,但后面就不一样了,过一段时间之后又会恢复到同步增长的状态。

直接赋值欧拉角

如果使用 Rotate 函数,也就是只在 update 里面写一句 transform.Rotate(new Vector3(20, 20, 0) * Time.deltaTime,Space.World); 那么结果会跟你想象的不太一样。可以看到他的旋转不仅 Z 轴是变化的,而且 X 和 Y 也没有保持统一。

使用 Rotate 函数

对比一下看看,旋转都是 new Vector3(20, 20, 0) * Time.deltaTime,Space.World;

[左]使用 Rotate [右]没使用 Rotate

先站立,再旋转

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotation : MonoBehaviour
{
    private Vector3 point1; // 正方体的一个顶点
    private Vector3 point2; // 正方体的相对的那个顶点
    private Quaternion targetRotation;
    private Quaternion originRotation;
    private float timeCount = 0.0f;
    private void Start()
    {
        Renderer render= GetComponent<Renderer>();
        point1 = render.bounds.center;
        point2 = render.bounds.center;
        point1 += render.bounds.extents; // 这里的 extents 是包含缩放的
        point2 -= render.bounds.extents;
        Vector3 origin_dir = point1 - point2;
        originRotation = transform.rotation;
        targetRotation = originRotation * Quaternion.FromToRotation(origin_dir, Vector3.up);
    }

    private void Update()
    {
        if(timeCount > 1) // 已经到位置了
        {
            // 写法一:
            transform.Rotate(new Vector3(0, 20 * Time.deltaTime, 0), Space.World);
            
            // 写法二:// 两种写法效果一样的
            // Vector3 axis = transform.InverseTransformDirection(Vector3.up); // 将世界方向的 up 转换到本地方向
            // transform.rotation *= Quaternion.AngleAxis(20 * Time.deltaTime, axis);
        }
        else // 没有旋转到对应位置
        {
            timeCount += Time.deltaTime;
            transform.rotation = Quaternion.Slerp(originRotation, targetRotation, timeCount);
        }
    }
}

运行结果

这个效果即使物体有缩放,也不受影响。先获得两个相对的顶点,然后算出应该旋转到的位置。Slerp 参考 lerp 也比较好理解。

相关文章

  • 物体的几种旋转方式

    一种错误的写法 可以看到物体只有一开始在 X 轴上是有旋转的,后面就 “卡住了”,具体的原因可以在学习了四元数与欧...

  • Unity物体旋转的方式

    第一种 transform.Rotate(new Vector3(90, 0, 0)); transform.Ro...

  • unity涨姿势知识(-)(技术面试题)

    前言 Let'Go 1,让一个物体围绕某一点旋转,有几种方法?分别是什么?旋转函数transform.Rotate...

  • 物体的旋转

    1.1物体的旋转 1.1.1对象的旋转方式 (1)transform.Rotate 应用一个欧拉角的旋转角度,eu...

  • Unity物体旋转方式学习笔记

    一、矩阵旋转:优点:旋转轴可以是任意向量缺点:旋转其实只需要知道一个向量+一个角度(共4个信息值),但矩阵却用了1...

  • iOS旋转处理的几种方式

    iOS的屏幕旋转处理有很多的方式,慢慢的在后续中一一记录一下 一、采用陀螺仪监听的方式 在项目中用到模仿小猿搜题拍...

  • android 图片旋转

    Android UI之ImageView旋转的几种方式 第一种,效率较低,不建议使用,即旋转bitmap: Bit...

  • Unity学习—坐标系与空间变换

    讲解的 Unity 中几种不同的坐标系与其之间的转换,以及汇总物体的移动和旋转方法 本文原地址:Unity学习—坐...

  • unity空间坐标相关知识

    unity空间坐标相关知识的整理。说到几个问题 几种屏幕坐标位置 几个常用的旋转函数 非等比缩放可能产生的物体扭曲...

  • Unity 物体旋转

    transform.rotation方式旋转[#2-%E5%BC%80%E5%8F%91%E7%8E%AF%E5%...

网友评论

      本文标题:物体的几种旋转方式

      本文链接:https://www.haomeiwen.com/subject/memtvktx.html