zoukankan      html  css  js  c++  java
  • unity-----------------------四元数与欧拉旋转方法

    转:http://blog.csdn.net/treepulse/article/details/49281295

    Transfrom.eulerAngles

    欧拉角

    public float yRotation = 5.0F;
    void Update() {
        yRotation += Input.GetAxis("Horizontal");
        transform.eulerAngles = new Vector3(10, yRotation, 0);
        //x轴和z轴的旋转角度始终不变,只改变绕y轴的旋转角度
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    Transfrom.rotation

    旋转角度

    //重设世界的旋转角度
    transform.rotation = Quaternion.identity;
    • 1
    • 2
    //平滑倾斜物体向一个target旋转
    public float smooth = 2.0F;
    public float tiltAngle = 30.0F;
    void Update() {
        float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
        float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
        Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
        //向target旋转阻尼
        transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Transfrom.RotateAround

    这里写图片描述

    //围绕世界坐标原点,每秒20度物体自旋
        transform.RotateAround (Vector3.zero, Vector3.up, 20 * Time.deltaTime);
    • 1
    • 2

    Transfrom.Rotate

    这里写图片描述
    这里写图片描述

    这里写图片描述

    //沿着x轴每秒1度慢慢的旋转物体
    transform.Rotate(Vector3.right * Time.deltaTime);
    //相对于世界坐标沿着y轴每秒1度慢慢的旋转物体
    transform.Rotate(Vector3.up * Time.deltaTime, Space.World);
    • 1
    • 2
    • 3
    • 4

    Transfrom.LookAt

    这里写图片描述 
    这里写图片描述 
    当该物体设置了LookAt并指定了目标物体时,该物体的z轴将始终指向目标物体,在设置了worldUp轴向时,该物体在更接近指定的轴向时旋转便的灵活,注意worldUp指的是世界空间,不论你物体在什么位置,只要接近指定的轴方向,旋转会变的更灵活。

    //这个完成的脚本附加到一个摄像机,使它连续指向另一个物体。
    //target变量作为一个属性显示在检视面板上
    //拖动另一个物体到这个上面,是摄像机注视它
    public Transform target;
    //每帧旋转摄像机,这样它保持注视目标
    void Update() {
        transform.LookAt(target);
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    // Point the object at the world origin
    //使物体注视世界坐标原点
    transform.LookAt(Vector3.zero);
    • 1
    • 2
    • 3

    Quaternion.LookRotation

    这里写图片描述

    //大多数时间可以使用transform.LookAt代替
    public Transform target;
    void Update() {
        Vector3 relativePos = target.position - transform.position;
        Quaternion rotation = Quaternion.LookRotation(relativePos);
        transform.rotation = rotation;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Quaternion.Angle

    这里写图片描述

    //计算transform和target之间的旋转角度
    public Transform target;
        void Update() {
            float angle = Quaternion.Angle(transform.rotation, target.rotation);
        }
    • 1
    • 2
    • 3
    • 4
    • 5

    Quaternion.Eulr

    这里写图片描述 
    这里写图片描述

    //绕y轴旋转30度
    public Quaternion rotation = Quaternion.Euler(0, 30, 0);
    • 1
    • 2

    Quaternion.Slerp

    这里写图片描述

    //在from和to之间插值旋转.
    //(from和to不能与附加脚本的物体相同)
    public Transform from;
    public Transform to;
    public float speed = 0.1F;
    void Update() {
        transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, Time.time * speed);
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    其实任何角度都可以没有限制

    Quaternion.FromToRotation

    这里写图片描述

    //设置旋转,变换的y轴转向z轴
    transform.rotation = Quaternion.FromToRotation (Vector3.up, transform.forward);
    • 1
    • 2
  • 相关阅读:
    备忘录方法与动态规划比较
    struct大小
    位域
    cocos2d-x获得系统的语言
    游戏中的心理学:利用归因错误让玩家更爱你的游戏
    武侠游戏核心追求点
    将“Cocos2dx-截屏并设置图片尺寸 ”中cocos2d-x代码转换为2.2的代码
    Cocos2dx-截屏并设置图片尺寸
    信息图:iOS 7开发者需要知道的事
    批处理学习 二
  • 原文地址:https://www.cnblogs.com/w-wfy/p/7616878.html
Copyright © 2011-2022 走看看