實現物體繞不同軸旋轉,並可以外部調用的函數

來源:互聯網
上載者:User

第一個檔案,聲明枚舉類型,分別為均勻變化和加速變化

using UnityEngine;using System.Collections;public enum CTRotationType{    Uniform,    AccelerateUniformly}

第二個檔案:主函數,實現圍繞軸變化的兩個函數,分別為均勻變化和加速變化

 

 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;    }}

第三個檔案,測試指令碼

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參數的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.