2014-09-10 19 views
0

我想知道如果有一種方式來增加(對我來說),120度每次我推「按鈕a」,再減去120度的的Z軸旋轉每次我推「ButtonB」, 2D精靈(預製)。統一 - 如何從一個角度添加或減去選定的度數?

這是我使用的是此刻的代碼,但它只能旋轉一次左,右後:提前

function TouchOnScreen() 
{ 
    if (Input.touchCount > 0) 
    { 
     var touch = Input.touches[0]; 
     if (touch.position.x < Screen.width/2) 
     { 
      var RSpeed = 10.0f 
      transform.rotation = Quaternion.Lerp (transform.rotation,Quaternion.Euler(0,0,120), Time.deltaTime*RSpeed); 
      Debug.Log("RotateRight"); 
     } 
     else if (touch.position.x > Screen.width/2) 
     { 
      var LSpeed = 10.0f 
      transform.rotation = Quaternion.Lerp (transform.rotation,Quaternion.Euler(0,0,-120), Time.deltaTime*LSpeed); 
      Debug.Log("RotateLeft"); 
     } 
    } 
} 

謝謝!

注意:如果可以,請使用unityscript,我對編碼相當陌生,unityscript是目前爲止我所知道的。從online documentation它表明該函數的簽名是

static function Lerp(from: Quaternion, to: Quaternion, t: float): Quaternion; 

,這意味着第二個參數

回答

0

爲對象的新的旋轉不旋轉偏移

你應該使用的東西排序

function TouchOnScreen() 
{ 
if (Input.touchCount > 0) 
{ 
    var touch = Input.touches[0]; 
    if (touch.position.x < Screen.width/2) 
    { 
     var RSpeed = 10.0f 
     transform.rotation = Quaternion.Lerp (transform.rotation,transform.rotation + Quaternion.Euler(0,0,120), Time.deltaTime*RSpeed); 
     Debug.Log("RotateRight"); 
    } 
    else if (touch.position.x > Screen.width/2) 
    { 
     var LSpeed = 10.0f 
     transform.rotation = Quaternion.Lerp (transform.rotation,transform.rotation + Quaternion.Euler(0,0,-120), Time.deltaTime*LSpeed); 
     Debug.Log("RotateLeft"); 
    } 
} 
} 

音符的第二個參數被transform.rotation + Quaternion.Euler(0,0,120)(當前旋轉+向右偏移)

我不是Unity引擎方面的專家(我纔開始使用免費版本昨天玩弄,直譯)

+0

感謝您的回答@workoverflow,但我得到這個錯誤:BCE0051:運算符'+'不能用於'UnityEngine.Quaternion'類型的左側和右側類型'UnityEngine.Quaternion'。 – Bartvb98 2014-09-11 13:44:53

0

試試這個

function TouchOnScreen() 
{ 
if (Input.touchCount > 0) 
{ 
    var touch = Input.touches[0]; 
    if (touch.position.x < Screen.width/2) 
    { 
     var RSpeed = 10.0f 
     transform.Rotate(0,0,120); 
     Debug.Log("RotateRight"); 
    } 
    else if (touch.position.x > Screen.width/2) 
    { 
     var LSpeed = 10.0f 
     transform.Rotate(0,0,-120); 
     Debug.Log("RotateLeft"); 
    } 
} 
} 

如果這不是工作與遊戲對象嘗試。我沒有檢查這個

+0

感謝您的回答@Rasa Mohamed,但這個腳本的問題是,旋轉不順利。它在一幀中從點A到點B捕捉。我想要它,以便旋轉是可見的。 – Bartvb98 2014-09-11 13:47:29