zoukankan      html  css  js  c++  java
  • 【转】Unity3D Transform中有关旋转的属性和方法测试

    Transform有关旋转个属性和方法测试

     

    一,属性

    1,var eulerAngles : Vector3

     

    [csharp] view plaincopy
     
     
    1. public float yRotation = 5.0F;  
    2.    void Update() {  
    3.        yRotation += Input.GetAxis("Horizontal");  
    4.        transform.eulerAngles = new Vector3(10, yRotation, 0);  
    5.    }  

    效果:与Quaternion.enlerAngles基本相同,用来设定物体的旋转角度,但不要分别设置xyz,要整体赋值。

     

     

    2,var rotation : Quaternion

     

    [csharp] view plaincopy
     
     
    1. public float smooth = 2.0F;  
    2.    public float tiltAngle = 30.0F;  
    3.    void Update() {  
    4.        float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;  
    5.        float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;  
    6.        Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);  
    7.        transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);  
    8.    }  

    效果:代表了物体的旋转,不能直接为transform.rotation.eulerAngles赋值。可以使用各种Quaternion的方法。

     

     

    二,方法

    1,function Rotate (eulerAngles : Vector3, relativeTo : Space = Space.Self) :void 

     

    [csharp] view plaincopy
     
     
    1. void Update() {  
    2.         transform.Rotate(Vector3.right * Time.deltaTime);  
    3.         transform.Rotate(Vector3.up * Time.deltaTime, Space.World);  
    4.     }  

     

    效果,使物体旋转一个基于欧拉角的旋转角度,eulerAngles.z度围绕z轴,eulerAngles.x度围绕x轴,eulerAngles.y度围绕y轴。vector3可以变成3个分开的float值。

     

    2,function Rotate (axis : Vector3, angle : float, relativeTo : Space = Space.Self) : void 

     

    [csharp] view plaincopy
     
     
    1. void Update() {  
    2.         transform.Rotate(Vector3.right, Time.deltaTime);  
    3.         transform.Rotate(Vector3.up, Time.deltaTime, Space.World);  
    4.     }  

     

    效果:按照angle度围绕axis轴旋转。

     

    3,function RotateAround (point : Vector3, axis : Vector3, angle : float) : void 

     

    [csharp] view plaincopy
     
     
    1.     public Transform target;   
    2.     Quaternion rotation;  
    3.     void Update()  
    4.     {  
    5.         transform.RotateAround(target.position, Vector3.up, 20 * Time.deltaTime);  
    6.     }  

    效果: 以point为中心点,以axis为轴进行旋转,类似于围绕某一点公转。

     

     

    4,function LookAt (target : Transform, worldUp :Vector3 = Vector3.up) : void 

     

    [csharp] view plaincopy
     
     
    1. public Transform target;  
    2.    void Update() {  
    3.        transform.LookAt(target);  
    4.    }  

    效果:使物体绕y轴旋转,z轴一直指向target,所以顾名思义叫lookat。

  • 相关阅读:
    NIO中几个非常重要的技术点
    NIO的epoll空轮询bug
    mysql支持跨表删除多条记录
    使用Fastjson生成Json字符串少字段属性(数据丢失)
    Linux系统下安装rz/sz命令及使用说明
    Slave_SQL_Running: No mysql同步故障
    二次幂权限设计
    spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
    XStream别名;元素转属性;去除集合属性(剥皮);忽略不需要元素
    JDBC通用DAO
  • 原文地址:https://www.cnblogs.com/mimime/p/6939707.html
Copyright © 2011-2022 走看看