第一个文件,声明枚举类型,分别为均匀变化和加速变化

1
2
3
4
5
6
7
8
using UnityEngine;
using System.Collections;
public enum CTRotationType
{
    Uniform,
    AccelerateUniformly
}

第二个文件:主函数,实现围绕轴变化的两个函数,分别为均匀变化和加速变化

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using UnityEngine;
using System.Collections;
public class CTRotation : MonoBehaviour {
 // Use this for initialization
 void Start () {
  
 }
  
 // Update is called once per frame
 void Update () {
        if (isRotating)
        {
            executeRotate();
        }
    }
    bool isRotating = false;
    Quaternion definedRotation = new Quaternion(0, 0, 0,0);
  
    Vector3 rotateVector = new Vector3(1,0,0);
  
    float rotateVelocity = 0;
  
 float accelerateDuration = 0;
 float leftDuration = 0;
 float rotateDuration = 0;
  
    int rotateAxis = 0;
 float angleRange = 0;
 float deltaRotate = 0;//0;
  
  
 // acceleration when it is in the accelerating process.
    float rotateAcceleration = 0;
    CTRotationType rotateType;
  
 //int RotateType = 0;
    private void initRotateArgument( float _initAngleRange, int _initRotateAxis, float _initRotateDuration)
    {
  rotateAxis = _initRotateAxis;
        rotateDuration = _initRotateDuration;
        leftDuration = _initRotateDuration;
  angleRange = _initAngleRange;
  rotateType = CTRotationType.Uniform;
    }
    public void RotateTo(float _angleRange, int _axis, float _duration)
    {
  print("in the rotateto");
        isRotating = false;
        rotateType = CTRotationType.Uniform;
  //RotateType = 0;
   
  initRotateArgument(_angleRange, _axis, _duration);
   
  switch(rotateAxis)
  {
   case 0: //rotate around X axis
   {
    rotateVector = Vector3.right;
    break;
   }
   case 1://rotate around Y axis
   {
    rotateVector = Vector3.up;
    break;
   }
   case 2://rotate around Z axis
   {
    rotateVector = Vector3.forward;
    break;
   }
   default:
    break;
  }
   
  deltaRotate = angleRange/rotateDuration;
   
        isRotating = true;
    }
    public void RotateTo(float _angleRange, int _axis, float _duration, float _accelerateDuration)
    {
        isRotating = false;
        rotateType = CTRotationType.AccelerateUniformly;
  //RotateType = 1;
  rotateAcceleration = 1/((rotateDuration - accelerateDuration)*accelerateDuration);
        initRotateArgument(_angleRange, _axis, _duration);
   
  switch(rotateAxis)
  {
   case 0: //rotate around X axis
   {
    rotateVector = Vector3.right;
    break;
   }
   case 1://rotate around Y axis
   {
    rotateVector = Vector3.up;
    break;
   }
   case 2://rotate around Z axis
   {
    rotateVector = Vector3.forward;
    break;
   }
   default:
    break;
  }
        accelerateDuration = _accelerateDuration;
   
     //   deltaRotate = angleRange/(_duration - _accelerateDuration*2);
        isRotating = true;
    }
    void executeRotate()
    {
        switch (rotateType)
        {
            //case 0://CTMoveType.Uniform:
   case CTRotationType.Uniform:
                uniformRotate();
                break;
            //case 1://CTMoveType.AccelerateUniformly:
   case CTRotationType.AccelerateUniformly:
                accelerateRotate();
                break;
        }
        
        leftDuration -= Time.deltaTime;
       /* if (leftDuration <= 0)
        {
            transform.position = targetPosition;
            isMoving = false;
        }*/
    }
    private void accelerateRotate()
    {
  print(leftDuration);
        if (leftDuration > (rotateDuration - accelerateDuration))
        {
   rotateVelocity = (float)((angleRange*(rotateDuration - leftDuration))*rotateAcceleration);
          //  transform.Rotate(rotateVelocity * Time.deltaTime*rotateVector, Space.World);
   transform.Rotate(rotateVelocity * rotateVector*Time.deltaTime, Space.World);
        }
        else if (leftDuration > accelerateDuration)
        {
   rotateVelocity = (float)((angleRange*accelerateDuration)*rotateAcceleration);
            transform.Rotate(rotateVelocity*rotateVector*Time.deltaTime, Space.World);
        }
        else if (leftDuration > 0)
        {
   rotateVelocity= (float)((angleRange*leftDuration)*rotateAcceleration);
   transform.Rotate(rotateVelocity*rotateVector*Time.deltaTime, Space.World);
        }
  else
   isRotating = false;
    }
    private void uniformRotate()
    {
  print(leftDuration);
  //if(leftDuration)
  if(leftDuration > 0)
  {
   transform.Rotate(rotateVector*deltaRotate*Time.deltaTime, Space.World);
   //transform.Rotate(rotateVector * Time.deltaTime*deltaRotate, Space.World);
  }
  else
   isRotating = false;
    }
}

第三个文件,测试脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using UnityEngine;
using System.Collections;
public class TestRotationScript : MonoBehaviour {
 // Use this for initialization
 void Start () {
  
 }
  
 // Update is called once per frame
 void Update () {
       
 }
 void OnGUI ()
    {
  CTRotation ttscript;
  CTChangeAlpha colorScript;
  GameObject testObject = GameObject.Find("TestCube");
        //Component testObjectScript = testObject.GetComponent("CRotation");
        ttscript = (CTRotation)testObject.GetComponent("CTRotation");
  colorScript = (CTChangeAlpha)testObject.GetComponent("CTChangeAlpha");
   
   if (GUI.Button (new Rect (20,40,80,20), "UniRotate")) {
       ttscript.RotateTo(3600f, 2, 2f);
  }
   
   if(GUI.Button(new Rect(20,60,80,20),"AccRotate")){
  ttscript.RotateTo(3600f, 2, 2f, 0.5f);
   }
   
    if(GUI.Button(new Rect(20,80,80,20),"Color")){
     colorScript.ColorTo(2,5.0f);
    }
    }
}

其中:第一个和第二脚本赋给目标物体;第三个脚本赋给任何一个物体作为测试物体使用

代码目的是方便外部调用和函数重用;注意isRotating参数的使用

实现物体绕不同轴旋转,并可以外部调用的函数相关推荐

  1. osg下物体绕自身轴旋转

    如果一个模型不在场景的中心点,这时候使用 osg::Matrix::rotate旋转的话,这个对象会围绕场景的中心点进行旋转,会转一个大圈,那么怎么做才能让他在任何位置的时候,围绕自己的轴心进行旋转? ...

  2. VTK笔记-几何变换-绕任意轴旋转

    绕任意轴旋转思路 中心轴与坐标轴平行   1.将旋转轴平移与坐标轴重合,物体也做平移操作:   2.物体绕坐标轴旋转:   3.执行步骤1的逆操作,将旋转轴平移回到原来位置,物体也对应平移: 中心轴与 ...

  3. 详解坐标变换矩阵 - 绕 x 轴旋转的旋转矩阵

    在高级驾驶辅助系统(ADAS)领域,存在多种常用的坐标系:LiDAR 坐标系.车辆坐标系.相机坐标系.图像坐标系等. 在高级驾驶辅助系统(ADAS)领域,存在多种常用的坐标系:LiDAR 坐标系.车辆 ...

  4. CocosCreator | 绕任意轴旋转/绕任意点旋转/平滑旋转/自定义环形体、胶囊体/面向目标位置

    01 效果演示 Cocos Creator 版本:3.4.1 该 demo 演示了行星自转(绕任意轴旋转).行星公转(绕任意点旋转).镜头拉近/复位(平滑旋转).行星环(自定义环形体).行星轴(自定义 ...

  5. glrotatef如何绕自身轴旋转

    首先我们知道所有的opengl操作都以点(0,0,0)作为基点. glrotatef(旋转角度(0~360),x,y,z),z,y,z是旋转轴.由于旋转操作全都围绕点(0,0,0),所以如果你的物体并 ...

  6. 18.外部相机校准——旋转(Rotation),R是什么样子的,绕Z轴旋转的例子,齐次坐标旋转_2

    目录 旋转(rotation) R是什么样子的 绕Z轴旋转的例子 齐次坐标旋转 旋转(rotation) 现在生活变得更加丑陋,旋转(rotation).我这里有一个图,或者我想这也是来自四边复合幻灯 ...

  7. 曲线绕x轴旋转曲面方程_绕x轴旋转(微积分旋转体绕y轴旋转体积~我看不懂图片上的公式~...)...

    关于空间曲线(参数方程)绕x轴旋转得到的曲面方程 绕哪个轴旋转,那个坐标不变,另一个的平方变,坐标的平方和绕轴旋转. 由一些在指定的集的数,称为参数或自变量,以决定因变量的结果.例如在运动学,参数通常 ...

  8. open3d显示pcd点云并读取任意点的坐标+生成点云绕任意轴旋转的transformation matrix

    为了对点云进行旋转操作,达到各点云之间不对齐的效果,找到了生成点云绕任意轴旋转的矩阵的代码. 链接: https://blog.csdn.net/u010848251/article/details/ ...

  9. 曲线绕x轴旋转曲面方程_曲线C绕y轴旋转所成的旋转曲面的方程为.PPT

    曲线C绕y轴旋转所成的旋转曲面的方程为 曲面之柱面.旋转面.椭球面 欧阳顺湘 北京师范大学 Recall 曲面方程(Equations for a Surface): 更多曲面 柱 面 旋转面 椭球面 ...

最新文章

  1. htcd816+android密码,HTC Desire 816刷机解锁教程
  2. python使用matplotlib可视化间断条形图、使用broken_barh函数可视化间断条形图、可视化定性数据的相同指标在时间维度上的差异
  3. BaseModelOutputWithPoolingAndCrossAttentions的API
  4. 最清晰的讲解各种梯度下降法原理与Dropout
  5. 成功解决AttributeError: ‘JointGrid‘ object has no attribute ‘annotate‘
  6. 2022-02-09
  7. mysql 单数据库设置编码,mysql数据库编码设置
  8. unity3D 实战 《小十传奇》系列之三:控制层(下)
  9. php完整系统由哪几部分,一个完整的物联网系统由哪些部分组成,各有什么功能?...
  10. 中科院计算所培训中心新一期javascript培训结束
  11. 百度文库收费文档下载方法
  12. Java获取姓名的首字母_java获取中文拼音首字母的实例
  13. 软件测试员工自述,软件测试人员的述职报告.docx
  14. 大数据-Kafka容错性
  15. php 微信支付跳不出来的,微信公众号 微信支付跳转空白
  16. 朱乐睿_校友风采_师范大学企业管理专业
  17. SQL Server 2008R2 企业版 百度云下载地址
  18. 论文笔记2:Combining Lexical, Syntactic, and Semantic Features with Maximum Entropy Models for Extracting
  19. win7安装python3.8失败_Python3 | Win7系统下无法安装问题解决
  20. h5+css3基础面试题

热门文章

  1. Boost:字符串裁剪Trim的测试程序
  2. ITK:将图像传递给函数
  3. ITK:在一张图像中设置像素值
  4. VTK:Utilities之CustomDenseArray
  5. VTK:PolyData之ColorCells
  6. VTK:Math之HomogeneousLeastSquares
  7. VTK:相互作用之WorldPointPicker
  8. VTK:图片之ImageContinuousDilate3D
  9. OpenCV进口重建Import Reconstruction
  10. Qt Creator将应用程序部署到QNX Neutrino设备