2012-05-17 44 views

回答

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

我相信你不能旋轉GUI紋理。我想最好的辦法是做一個你想做的事情,就是將一架飛機作爲一個孩子分配給面向它的攝像機,然後根據需要旋轉該飛機。