实现物体绕不同轴旋转,并可以外部调用的函数
第一个文件,声明枚举类型,分别为均匀变化和加速变化
| 1 2 3 4 5 6 7 8 | usingUnityEngine;usingSystem.Collections;publicenumCTRotationType{    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 | usingUnityEngine;usingSystem.Collections;publicclassCTRotation : MonoBehaviour { // Use this for initialization voidStart () {  }  // Update is called once per frame voidUpdate () {        if(isRotating)        {            executeRotate();        }    }    boolisRotating = false;    Quaternion definedRotation = newQuaternion(0, 0, 0,0);     Vector3 rotateVector = newVector3(1,0,0);     floatrotateVelocity = 0;  floataccelerateDuration = 0; floatleftDuration = 0; floatrotateDuration = 0;     introtateAxis = 0; floatangleRange = 0; floatdeltaRotate = 0;//0;   // acceleration when it is in the accelerating process.    floatrotateAcceleration = 0;    CTRotationType rotateType;  //int RotateType = 0;    privatevoidinitRotateArgument( float_initAngleRange, int_initRotateAxis, float_initRotateDuration)    {  rotateAxis = _initRotateAxis;        rotateDuration = _initRotateDuration;        leftDuration = _initRotateDuration;  angleRange = _initAngleRange;  rotateType = CTRotationType.Uniform;    }    publicvoidRotateTo(float_angleRange, int_axis, float_duration)    {  print("in the rotateto");        isRotating = false;        rotateType = CTRotationType.Uniform;  //RotateType = 0;    initRotateArgument(_angleRange, _axis, _duration);    switch(rotateAxis)  {   case0: //rotate around X axis   {    rotateVector = Vector3.right;    break;   }   case1://rotate around Y axis   {    rotateVector = Vector3.up;    break;   }   case2://rotate around Z axis   {    rotateVector = Vector3.forward;    break;   }   default:    break;  }    deltaRotate = angleRange/rotateDuration;          isRotating = true;    }    publicvoidRotateTo(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)  {   case0: //rotate around X axis   {    rotateVector = Vector3.right;    break;   }   case1://rotate around Y axis   {    rotateVector = Vector3.up;    break;   }   case2://rotate around Z axis   {    rotateVector = Vector3.forward;    break;   }   default:    break;  }        accelerateDuration = _accelerateDuration;       //   deltaRotate = angleRange/(_duration - _accelerateDuration*2);        isRotating = true;    }    voidexecuteRotate()    {        switch(rotateType)        {            //case 0://CTMoveType.Uniform:   caseCTRotationType.Uniform:                uniformRotate();                break;            //case 1://CTMoveType.AccelerateUniformly:   caseCTRotationType.AccelerateUniformly:                accelerateRotate();                break;        }               leftDuration -= Time.deltaTime;       /* if (leftDuration <= 0)        {            transform.position = targetPosition;            isMoving = false;        }*/    }    privatevoidaccelerateRotate()    {  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);        }        elseif(leftDuration > accelerateDuration)        {   rotateVelocity = (float)((angleRange*accelerateDuration)*rotateAcceleration);            transform.Rotate(rotateVelocity*rotateVector*Time.deltaTime, Space.World);        }        elseif(leftDuration > 0)        {   rotateVelocity= (float)((angleRange*leftDuration)*rotateAcceleration);   transform.Rotate(rotateVelocity*rotateVector*Time.deltaTime, Space.World);        }  else   isRotating = false;    }    privatevoiduniformRotate()    {  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 | usingUnityEngine;usingSystem.Collections;publicclassTestRotationScript : MonoBehaviour { // Use this for initialization voidStart () {  }  // Update is called once per frame voidUpdate () {       } voidOnGUI ()    {   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 (newRect (20,40,80,20), "UniRotate")) {       ttscript.RotateTo(3600f, 2, 2f);  }     if(GUI.Button(newRect(20,60,80,20),"AccRotate")){  ttscript.RotateTo(3600f, 2, 2f, 0.5f);   }      if(GUI.Button(newRect(20,80,80,20),"Color")){     colorScript.ColorTo(2,5.0f);    }    }} | 
其中:第一个和第二脚本赋给目标物体;第三个脚本赋给任何一个物体作为测试物体使用
代码目的是方便外部调用和函数重用;注意isRotating参数的使用
    失败是什么?没有什么,只是更走近成功一步;成功是什么?就是走过了所有通向失败的路,只剩下一条路,那就是成功的路。
 
                     
                    
                 
                    
                 
                
            
         
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号