绕着一个点旋转 :

  1. transform.RotateAround(Vector3.zero, Vector3.up, speed* Time.deltaTime );

第一个参数,点的位置。第二个参数,法线方向,第三个参数,速度.如图时针旋转。


旋转固定角度

  1. gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, Quaternion.Euler(,, ), );

第一个参数起始角度,第二参数结束角度,第三个参数旋转工消耗的时间。


static function LookRotation (forward : Vector3, upwards : Vector3 = Vector3.up) : Quaternion

创建一个旋转,沿着forward(z轴)并且头部沿着upwards(y轴)的约束注视。也就是建立一个旋转,使z轴朝向view  y轴朝向up。

  1. public Transform target;
  2. void Update() {
  3. Vector3 relativePos = target.position - transform.position;
  4. Quaternion rotation = Quaternion.LookRotation(relativePos);
  5. transform.rotation = rotation;
  6. }

WSAD旋转,阻尼旋转

    float smooth = 2.0f;
    float tiltAngle = 30.0f;

  1.    void Update()
  2. {
  3. float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
  4. float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
  5. var target = Quaternion.Euler(tiltAroundX, , tiltAroundZ);
  6. // Dampen towards the target rotation
  7. //向target旋转阻尼
  8. transform.rotation = Quaternion.Slerp(transform.rotation, target,
  9. Time.deltaTime * smooth);
  10. }

鼠标点着旋转1:

  1. private Transform hitTransfrom;
  2.  
  3. void Update()
  4. {
  5. if (Input.GetMouseButtonDown())
  6. {
  7. RaycastHit hit;
  8. Ray mouseray = Camera.main.ScreenPointToRay(Input.mousePosition);
  9. if (Physics.Raycast(mouseray, out hit))
  10. {
  11. hitTransfrom = hit.transform;
  12. }
  13. }
  14. else if (Input.GetMouseButtonUp())
  15. {
  16. hitTransfrom = null;
  17. }
  18. if (hitTransfrom)
  19. {
  20. Matrix4x4 localmatrix = hitTransfrom.worldToLocalMatrix;
  21. Vector3 vUp = localmatrix.MultiplyVector(new Vector3(, , ));
  22. Vector3 vRight = -localmatrix.MultiplyVector(new Vector3(, , ));
  23. float fMoveX = -Input.GetAxis("Mouse X") * Time.deltaTime * 200.0f;
  24. Quaternion rotation = Quaternion.AngleAxis(fMoveX, vUp);
  25. hitTransfrom.localRotation *= rotation;
  26. float fMoveY = -Input.GetAxis("Mouse Y") * Time.deltaTime * 200.0f;
  27. Quaternion rotoy = Quaternion.AngleAxis(fMoveY, vRight);
  28. hitTransfrom.localRotation *= rotoy;
  29. }
  30. }

鼠标点着旋转2:

  1. void Update()
  2. {
  3. if (Input.GetMouseButtonDown())
  4. {
  5. OnMouseDrag();
  6. }
  7. }
  8. void OnMouseDrag()
  9. {
  10. this.transform.Rotate(new Vector3(Input.GetAxis("Mouse Y"), -Input.GetAxis("Mouse X"), ) * 6f, Space.World);
  11. }

缩放的代码,缩放、自动旋转、拖拽物体

  1. private bool onDrag = false;
  2. public float speed = 6f;
  3. private float tempSpeed;
  4. private float axisX;
  5. private float axisY;
  6. private float cXY;
  7.  
  8. void OnMouseDown(){
  9. axisX=0f;
  10. axisY=0f;
  11. }
  12. void OnMouseDrag ()
  13. {
  14. onDrag = true;
  15. axisX = -Input.GetAxis ("Mouse X");
  16. axisY = Input.GetAxis ("Mouse Y");
  17. cXY = Mathf.Sqrt (axisX * axisX + axisY * axisY);
  18. if(cXY == 0f){
  19. cXY=1f;
  20. }
  21. }
  22.  
  23. float Rigid ()
  24. {
  25. if (onDrag) {
  26. tempSpeed = speed;
  27. } else {
  28. if (tempSpeed > ) {
  29. tempSpeed -= speed* * Time.deltaTime / cXY;
  30. } else {
  31. tempSpeed = ;
  32. }
  33. }
  34. return tempSpeed;
  35. }
  36.  
  37. void Update ()
  38. {
  39. gameObject.transform.Rotate (new Vector3 (axisY, axisX, ) * Rigid (), Space.World);
  40. if (!Input.GetMouseButton ()) {
  41. onDrag = false;
  42. }
  43. }
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /**
  5.  
  6. * @Function:使物体自动旋转,鼠标能够360°拖转物体,托转物体的时候,自动旋转停止,同时滚轮实现物体的缩放功能
  7. * @Ahthor:黄杰
  8. * @Date:2013-04-24
  9.  
  10. */
  11. public class ZoomAndDrag : MonoBehaviour {
  12.  
  13. public Camera MainCamera;
  14. public float ZoomMin; //滚轮的最小值
  15. public float ZoomMax; //滚轮的最大值
  16. private float normalDistance; //设置摄像机的景深值
  17. private float MouseWheelSencitivity = 10.0f; //鼠标灵敏度,就是缩放的速度的快慢
  18.  
  19. private float axisX;
  20. private float axisY;
  21. public float speed = 6f;
  22. private float tempSpeed;
  23.  
  24. private bool RoationOnly;
  25.  
  26. void Start ()
  27. {
  28. normalDistance = 50.0f;
  29. ZoomMin = 20.0f;
  30. ZoomMax = 100.0f;
  31. RoationOnly = true;
  32. }
  33.  
  34. void Update ()
  35. {
  36. Roation();
  37. Zoom();
  38. this.transform.Rotate(new Vector3(axisY, axisX, ) * Rigid(), Space.World); //物体旋转的方法
  39. }
  40.  
  41. //自动旋转物体的方法,放在Update中调用
  42. void Roation()
  43. {
  44. if (RoationOnly)
  45. {
  46. gameObject.transform.Rotate(Vector3.up * Time.deltaTime * );
  47. }
  48. }
  49.  
  50. /****
  51. *鼠标滚轮缩放物体的方法
  52. *
  53. * **/
  54. void Zoom()
  55. {
  56. if (Input.GetAxis("Mouse ScrollWheel") != )
  57. {
  58. if (normalDistance >= ZoomMin && normalDistance <= ZoomMax)
  59. {
  60. normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSencitivity;
  61. }
  62. if (normalDistance < ZoomMin)
  63. {
  64. normalDistance = ZoomMin;
  65. }
  66. if (normalDistance > ZoomMax)
  67. {
  68. normalDistance = ZoomMax;
  69. }
  70.  
  71. MainCamera.fieldOfView = normalDistance;
  72. }
  73. }
  74. /***
  75. *
  76. * 鼠标左键控制物体360°旋转+惯性
  77. * **/
  78. float Rigid()
  79. {
  80. if (Input.GetMouseButton())
  81. {
  82. RoationOnly = false; //当鼠标按下的时候,停止自动旋转
  83.  
  84. axisX = Input.GetAxis("Mouse X");
  85. axisY = Input.GetAxis("Mouse Y");
  86. if (tempSpeed < speed)
  87. {
  88. tempSpeed += speed * Time.deltaTime * ;
  89. }
  90. else
  91. {
  92. tempSpeed = speed;
  93. }
  94. }
  95. else
  96. {
  97. RoationOnly = true; //当鼠标没有按下的时候,恢复自动旋转
  98. if (tempSpeed > )
  99. {
  100. tempSpeed -= speed * Time.deltaTime;
  101. }
  102. else
  103. {
  104. tempSpeed = ;
  105. }
  106. }
  107. return tempSpeed;
  108. }
  109. }

unity的旋转的更多相关文章

  1. [转]Unity 3D旋转矢量方向及二维平面基于一点选择另一点(Rotate a Vector3 direction & Rotate a point about another point in 2D )

    http://specialwolf.blog.163.com/blog/static/124466832201301332432766/ ****************************** ...

  2. unity 对象旋转,自转

    1.对象具体的围绕哪个轴旋转,对应的设置值: transform.Rotate(new Vector3(1,0,0));  //绕x轴旋转    //默认是物体围绕世界坐标的XYZ轴旋转,即物体绕着世 ...

  3. Unity 摄像机旋转初探

    接触打飞机的游戏时都会碰见把摄像机绕 x 轴顺时针旋转 90°形成俯瞰的视角的去看飞船.也没有多想,就感觉是坐标系绕 x 轴旋转 90°完事了.但是昨天用手比划发一下发现不对.我就想这样的话绕 x 轴 ...

  4. Blender模型导入进Unity,旋转缩放的调整

    Blender跟Unity的XYZ轴不同的原因,导致Blender模型导入Unity之后会发生模型朝向不对. 请先看看下边这个情况: 首先,Blender物体模式下,对模型进行 旋转 缩放,将会在右边 ...

  5. Unity的旋转-四元数,欧拉角用法简介

    当初弄不明白旋转..居然找不到资料四元数应该用轴角相乘...后来自己摸明白了 通过两种旋转的配合,可以告别世界空间和本地空间矩阵转换了,大大提升效率. 每个轴相乘即可,可以任意轴,无限乘.无万向节锁问 ...

  6. Unity 物体旋转会发生变形

    当游戏对象的 "父物体们" 有一个是缩放的,也就是Scale不是(1,1,1)的时候,旋转这个游戏对象它就会出现变形的现象.

  7. unity 角色旋转

    using UnityEngine; using System.Collections; public class Triangle : MonoBehaviour { public float sp ...

  8. Unity 逐步旋转

    npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation, Quaternion.LookRotation(moveDir), ...

  9. Unity 360 旋转 缩放

    using UnityEngine; using System.Collections; public class SandR : MonoBehaviour { public GameObject ...

随机推荐

  1. What Influences Method Call Performance in Java?--reference

    reference from:https://www.voxxed.com/blog/2015/02/too-fast-too-megamorphic-what-influences-method-c ...

  2. Regular Expressions --正则表达式官方教程

    http://docs.oracle.com/javase/tutorial/essential/regex/index.html This lesson explains how to use th ...

  3. linux crt

    1.仿真  终端选linux  ANSI颜色[有颜色了] 使用颜色方案[颜色加深了] 2.外观  选传统的 ,utf-8 就不会乱码了

  4. View绘制详解(四),谝一谝layout过程

    上篇博客我们介绍了View的测量过程,这只是View显示过程的第一步,第二步就是layout了,这个我们一般译作布局,其实就是在View测量完成之后根据View的大小,将其一个一个摆放在ViewGro ...

  5. Javascript之基本包装类型

    一.基本包装类型概述 var box = 'Mr. Lee';//定义一个字符串 var box2 = box.substring(2);//截掉字符串前两位 alert(box2);//输出新字符串 ...

  6. JAVA_JSON_example

    package cn.kjxy.JSON; import java.util.List; import org.json.JSONArray; import org.json.JSONExceptio ...

  7. C#总结1

    C#摘要 第一章: 数据类型: 在定义变量的时候,记下规则,开头不能是数字,变量名只能包括 字母 “_” 数字 整型类型 名称 CTS类型 说明 范围 sbyte System.SByte 8位有符号 ...

  8. 为Photoshop添加右键快捷

    打开注册表,开始--->运行--->regedit 找到  HKEY_CLASSES_ROOT  <----> *<---->shell 新建项,使用Photosh ...

  9. oracle添加表字段跟修改表字段属性

    添加字段 alter table mid_contactinfo add(status varchar(20),createdate varchar(50),modifydate varchar(50 ...

  10. sql server 2008 r2 清除数据库日志

    USE [master] GO ALTER DATABASE [数据库名] SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE [数据库名] SET ...