使用touchmove将texture2d旋转到540度

时间:2012-05-17 04:53:52

标签: unity3d

我的gui屏幕上有一个纹理2d。我想用手指触摸旋转将此纹理旋转到+540和-540度。超出此限制,它应该停止旋转。 可以帮助吗?

2 个答案:

答案 0 :(得分:1)

我相信你无法旋转GUI纹理。我认为做你想做的最好的方法是将一架飞机作为一个孩子分配给面对它的相机,并根据需要旋转那个平面。

答案 1 :(得分:0)

public Texture2D steering;
    public static TouchControl Instance;
    public float angleRotate=0f;
    public float carRotation=0f;
    public int finger;


    private Matrix4x4 savedMatrix;  
    private float steerAngleRad;
    public float steerAngleDeg;
    private float steerAngleRadTemp;
    public float steerAngleDegTemp;
    private float dx=0f;
    private float dy=0f;
    private int i;
    private float newAngle =0f;
    private float initAngle;
    private float rotationLimit=540f;
    public float secondWheelRot=0f;
    private float preRotDeg=0f;
    private float angleDiff=0f;
    private bool clockWheel=false;
    private bool onlyOnce=false;

    private Vector2 steeringPos;
    private Vector2 centre;
    private bool inXRange=false;
    private bool inYRange=false;
//  private bool moving=false;
    private float steeringWidth=0f;
    private float steeringHeight=0f;

    private float gobackSpeed=20f;

    void Start()
    {
        Instance=this;
        i=0;
        finger=3;
        steeringPos=new Vector2(Screen.width*0.025f,Screen.height*0.68f);
        steeringWidth=Screen.width*0.12f;
        steeringHeight=steeringWidth;
        centre = new Vector2(steeringPos.x + steeringWidth / 2, steeringPos.y + steeringHeight / 2);
    }

    void Update()
    {

        if(Input.touchCount>0 && (!PauseButton.pauseFlag && !DamageBar.Instance.levelLost && !GameTimer.Instance.levelLost && !CommonIndicator.Instance.levelComplete))
        {   
            for(i=0;i< Input.touchCount; ++i)
            {
                if(Input.GetTouch(i).position.x > 0f && Input.GetTouch(i).position.x < steeringWidth+Screen.width*0.1f)
                {
                    inXRange=true;
                }
                else
                {
                    inXRange=false;
                }
                if(Input.GetTouch(i).position.y > 0f && Input.GetTouch(i).position.y < Screen.height-(steeringPos.y))
                {
                    inYRange=true;
                }
                else
                {
                    inYRange=false;
                }

                ///////////////if touch is in the range/////////////////////
                if(inXRange && inYRange)
                {
                    if(Input.GetTouch(i).phase==TouchPhase.Began)
                    {
                        finger=Input.touches[i].fingerId;
                        dx=Input.GetTouch(i).position.x-centre.x;
                        dy=Input.GetTouch(i).position.y-(Screen.height-centre.y);
                        steerAngleRadTemp=Mathf.Atan2(dy,dx);
                        steerAngleDegTemp=-steerAngleRadTemp*Mathf.Rad2Deg;
                    }
                    if((Input.GetTouch(i).phase==TouchPhase.Stationary || Input.GetTouch(i).phase==TouchPhase.Moved))
                    {
                        preRotDeg=newAngle;
                        if(preRotDeg<-180f && preRotDeg>-360f)
                        {
                            preRotDeg+=360f;
                        }
                        if(preRotDeg>180f && preRotDeg<360f)
                        {
                            preRotDeg-=360f;
                        }
                        dx=Input.GetTouch(i).position.x-centre.x;
                        dy=Input.GetTouch(i).position.y-(Screen.height-centre.y);
                        steerAngleRad=Mathf.Atan2(dy,dx);
                        steerAngleDeg=-steerAngleRad*Mathf.Rad2Deg;
                        angleRotate=steerAngleDeg-steerAngleDegTemp;
                        newAngle=angleRotate;
                        if(newAngle<-180f && newAngle>-360f)
                        {
                            newAngle+=360f;
                        }
                        if(newAngle>180f && newAngle<360f)
                        {
                            newAngle-=360f;
                        }
                        if(!onlyOnce)
                        {
                            onlyOnce=true;
                            if(newAngle>preRotDeg)
                                clockWheel=true;
                            else
                                clockWheel=false;
                        }
                        if(clockWheel && preRotDeg>0f && newAngle < 0f)
                        {
                            clockWheel=true;
                        }
                        else if(clockWheel && preRotDeg<0f && newAngle >0f)
                        {
                            clockWheel=true;
                        }
                        else if(!clockWheel && preRotDeg>0f && newAngle <0f)
                        {
                            clockWheel=false;
                        }
                        else if(!clockWheel && preRotDeg<0f && newAngle >0f)
                        {
                            clockWheel=false;
                        }
                        else if(newAngle>preRotDeg)
                        {
                            clockWheel=true;
                        }
                        else if(newAngle<preRotDeg)
                        {
                            clockWheel=false;
                        }
                        if(Mathf.Abs(newAngle)>Mathf.Abs(preRotDeg))
                        {
                            angleDiff=Mathf.Abs(newAngle)-Mathf.Abs(preRotDeg);
                        }
                        else
                        {
                            angleDiff=Mathf.Abs(preRotDeg)-Mathf.Abs(newAngle);
                        }
                        if(clockWheel)
                        {
                            carRotation+=angleDiff;
                            secondWheelRot+=angleDiff;
                            if(secondWheelRot>rotationLimit)
                            {
                                secondWheelRot=rotationLimit;
                            }
                        }
                        else
                        {
                            carRotation-=angleDiff;
                            secondWheelRot-=angleDiff;
                            if(secondWheelRot<-rotationLimit)
                            {
                                secondWheelRot=-rotationLimit;
                            }   
                        }
                    }
//                  else
//                  {
//                      steeringToZero();                       
//                  }
                    if(Input.GetTouch(i).phase==TouchPhase.Ended)// && Input.touches[i].fingerId != AccControl.Instance.finger)
                    {
                        onlyOnce=false;
                        carRotation=0f;
                        angleRotate=0f;
                        newAngle=0f;
                        steeringToZero();
                        finger=3;
                    }
                }
                else if(!AccControl.Instance.onPadTouch && finger==3)
                {
                    steeringToZero();                       
                }
            }
        }
        else if(Input.touchCount==0)
        {
            steeringToZero();
        }
        if(Input.touchCount==1 && AccControl.Instance.onPadTouch)
        {
            steeringToZero();
        }
        if (Input.GetKeyDown(KeyCode.Escape) && !PauseButton.pauseFlag) 
        { 
            Application.LoadLevel("MainMenu");
        }
    }
    void steeringToZero()
    {
        if(secondWheelRot>0)
        {
            secondWheelRot-=gobackSpeed/(Input.touchCount+1);
        }
        else if(secondWheelRot<0)
        {
            secondWheelRot+=gobackSpeed/(Input.touchCount+1);
        }
        if(secondWheelRot<gobackSpeed && secondWheelRot>-gobackSpeed)
        {
            secondWheelRot=0f;
        }

    }

    void OnGUI() 
    {
        savedMatrix = GUI.matrix;
        GUIUtility.RotateAroundPivot(carRotation,centre);
        GUI.DrawTexture(new Rect(steeringPos.x,steeringPos.y, steeringWidth, steeringHeight), steering);
        GUI.matrix = savedMatrix;
    }