2014-04-18 73 views
1

我想使用Input.acceleration旋轉floor。如果Input.acceleration接近於零,我想在玩家站立的位置旋轉地板,並限制我在某些角度的旋轉也停在0。考慮到我在遊戲編程新手,我想出了這個代碼:Unity使用Input.acceleration旋轉Foor

using UnityEngine; 
using System.Collections; 

public class Tilt : MonoBehaviour { 

    public float maxRotationAngle = 350;     // max rotation right 
    public float minRotationAngle = 10;      // max rotation left 
    public float rotationSpeed = 20;      //rotation speed 
    public Transform rotateAround;       //rotation point 
    private bool stopRotation = false;      //if this is true rotation stops 
    private int stopDir;         //direction where rotation stops -1 equals left 0 center 1 right 

    void Update() { 

     int tiltDir = 0;         //input tilt direction 
     float accel = Input.acceleration.x;     //input tilt value 
     float currentRotation = transform.eulerAngles.z; //current rotation 

     //set rotation direction 
     if (accel > 0) { 
      tiltDir = 1; 
     }else if (accel < 0){ 
      tiltDir = -1; 
     } 

     //stop rotation left 
     if (!stopRotation && (currentRotation < maxRotationAngle && currentRotation > 270)) { 
      stopRotation = true; 
      stopDir = -1; 
     } 
     //stop rotation right 
     if (!stopRotation && (currentRotation > minRotationAngle && currentRotation < 270)) { 
      stopRotation = true; 
      stopDir = 1; 
     } 
     //allow rotation right 
     if (stopRotation && stopDir < 0 && Input.acceleration.x > 0) { 
      stopRotation = false; 

     } 
     //allow rotation left 
     if (stopRotation && stopDir > 0 && Input.acceleration.x < 0) { 
      stopRotation = false; 
     } 
     //stop rotation center 
     if(!stopRotation && currentRotation < 0.2 || (currentRotation > 359.8 && currentRotation < 360)){ 
      if(accel > -0.1 && accel < 0.1){ 
       stopRotation = true; 
       stopDir = 0; 
      } 
     } 
     //allow rotation from center 
     if(stopRotation && stopDir == 0 && (accel < -0.1 || accel > 0.1)){ 
      stopRotation = false; 
     } 
     //apply rotation 
     if(!stopRotation){ 
      transform.RotateAround(rotateAround.position, new Vector3(0, 0, tiltDir), rotationSpeed * Mathf.Abs(accel) * Time.deltaTime); 
     } 

    } 
} 

這是工作,但這種方法並不準確,我認爲有這樣做的更便宜的方法。那麼有更好的方法嗎?

+0

你真的需要旋轉地板嗎?旋轉播放器本身不是更容易嗎? – kreys

+0

不,因爲我希望物理對象根據其旋轉取決於地面。 – user3548661

+0

爲什麼minRotationAngle大於0? minRotationAngle和maxRotationAngle在-180和180度之間會不會更有意義?那麼在這種情況下分別是-170和170? –

回答

0

我想出了這個,我認爲這完成了你想要的,儘管我可能誤解了你的代碼。我已經刪除了你的魔術數字,改變了角度以在-180和180之間映射,並將你的變量重命名爲具有更好可維護性的全名。

public float maxRotationAngle = 170; 
public float minRotationAngle = -170; 
public float minimumAcceleration = 0.1f; 
public float rotationSpeed = 20; 
public Transform rotateAroundTransform; 

void Update() 
{ 
    float deltaAcceleration = Mathf.Abs(Input.acceleration.x); 
    float currentRotation = transform.eulerAngles.z; 

    //stop rotation outside of angle range and motion range 
    if (currentRotation > minRotationAngle && 
     currentRotation < maxRotationAngle && 
     deltaAcceleration < minimumAcceleration) 
    { 
     //set rotation direction 
     int tiltDirection = Input.acceleration.x > 0 ? 1 : -1; 
     transform.RotateAround(rotateAroundTransform.position, new Vector3(0, 0, tiltDirection), rotationSpeed * deltaAcceleration * Time.deltaTime); 
    } 
} 

希望有幫助!

+0

是的,它幫助。謝謝。 – user3548661

+0

我很高興它有幫助!請使用投票按鈕下方的勾號標記此答案爲正確。謝謝。 –

+0

我不能。我沒有足夠的聲譽抱歉。 – user3548661