2017-05-02 83 views
-1

飛船從A點開始移動。飛船正面向移動方向。 現在,當我點擊L鍵上的一個時,我希望飛船將旋轉並將面向原始位置,然後開始移動。但即使宇宙飛船現在由Z軸或Y或X旋轉,首先將其旋轉到正常旋轉值並面對起始移動位置。 Explain如何將太空船旋轉回原來的旋轉狀態以面對原始位置?

using UnityEngine; 
using System.Collections; 

public class Control : MonoBehaviour 
{ 
    public int rotationSpeed = 75; 
    public int movementspeed = 10; 
    public int thrust = 10; 

    private bool isPKeyDown = false; 
    private float acceleration = .0f; 
    private Vector3 previousPosition = Vector3.zero; 
    private Rigidbody _rigidbody; 
    private Vector3 originalPosition; 
    private Quaternion originalRotation; 

    // Use this for initialization 
    void Start() 
    { 
     originalPosition = transform.position; 
     originalRotation = transform.rotation; 

     _rigidbody = GetComponent<Rigidbody>(); 
     Debug.Log("Acc Speed: " + thrust); 
    } 

    // Update is called once per frame 
    void Update() 
    { 

     var v3 = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal"), 0.0f); 
     transform.Rotate(v3 * rotationSpeed * Time.deltaTime); 
     transform.position += transform.forward * Time.deltaTime * movementspeed; 

     if (Input.GetKey(KeyCode.Z)) 
      transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime); 

     if (Input.GetKey("p")) 
     { 
      isPKeyDown = Input.GetKey("p"); 
      float distance = Vector3.Distance(previousPosition, transform.position); 
      acceleration = distance/Mathf.Pow(Time.deltaTime, 2); 

      previousPosition = transform.position; 
      _rigidbody.AddRelativeForce(0f, 0f, thrust, ForceMode.Acceleration); 
     } 

     if (Input.GetKey("l")) 
     { 
      transform.rotation = Quaternion.Slerp(transform.rotation, originalRotation, 0); 
      //StartCoroutine(TurnShip(transform, transform., originalRotation.eulerAngles, 1)); 
      //transform.position += transform.forward * Time.deltaTime * movementspeed; 
     } 
    } 

    IEnumerator TurnShip(Transform ship, Vector3 startAngle, Vector3 endAngle, float smooth) 
    { 
     float lerpSpeed = 0; 

     while (lerpSpeed < 1) 
     { 
      ship.eulerAngles = Vector3.Lerp(startAngle, endAngle, lerpSpeed); 
      lerpSpeed += Time.deltaTime * smooth; 
      yield return null; 
     } 
    } 

    void OnGUI() 
    { 
     if (isPKeyDown) 
     { 
      GUI.Label(new Rect(100, 100, 200, 200), "Acc Speed: " + acceleration); 
     } 
    } 
} 

這是我點擊L鍵,但我嘗試了一些東西,但仍無法找到如何做到這一點。

主要目標是如果我在L上點擊一次,如果需要,飛船應該自動旋轉並移回原始位置,然後降落在地面上。 L代表着陸,這是主要目標。

回答

1

添加一個變量之上 -

... 
private Vector3 originalPosition; 
private Quaternion originalRotation; 

private bool landShip = false; 
... 

而且在update函數中使用下面的代碼 -

if (Input.GetKey("l")) 
{ 
    landShip = true; 
    //StartCoroutine(TurnShip(transform, transform., originalRotation.eulerAngles, 1)); 
    //transform.position += transform.forward * Time.deltaTime * movementspeed; 
} 
if(landShip){ 
    transform.rotation = Quaternion.Slerp(transform.rotation, originalRotation, 0.5f); 
} 

一旦飛船土地,設置landShip值回false