2017-06-23 36 views
0

底座是一個紅色的立方體。 遊戲開始時,宇宙飛船已經在移動。 當我點擊/按下L按鈕時,宇宙飛船旋轉而面向基地並開始向基地移動,但當基地靠近基地時,它的行爲出乎意料,飛船開始在基地不停地滾動。我怎樣才能讓飛船自動着陸?

我想要的是讓這個着陸自動像攪拌機的這個youtube視頻一樣。 我不想要圖形,但它的着陸方式。

Blender landing spaceship

這是顯示我的飛船一個短片時,它開始登陸:

Landing test

這是我使用,用於控制飛船的腳本和着地部分應是自動的。 腳本附在飛船上。

using UnityEngine; 
using System.Collections; 

public class ControlShip : MonoBehaviour { 

    public int rotationSpeed = 75; 
    public int movementspeed = 10; 
    public int thrust = 10; 
    public float RotationSpeed = 5; 

    private bool isPKeyDown = false; 
    private float acceleration = .0f; 
    private Vector3 previousPosition = Vector3.zero; 
    private Rigidbody _rigidbody; 
    private bool landing = false; 
    private Vector3 originPosition; 
    private Vector3 lastPosition; 
    private const float minDistance = 0.2f; 
    private Transform baseTarget; 

    // Use this for initialization 
    void Start() { 
     baseTarget = GameObject.Find("Base").transform; 
     originPosition = transform.position; 
     _rigidbody = GetComponent<Rigidbody>(); 
     Debug.Log("Acc Speed: " + thrust); 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     if (landing == false) 
     { 
      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(KeyCode.R)) 
       transform.Rotate(Vector3.right * rotationSpeed * Time.deltaTime); 

      if (Input.GetKey(KeyCode.P)) 
      { 
       isPKeyDown = Input.GetKey(KeyCode.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); 
      } 
     } 
     else 
     { 
      transform.position += transform.forward * Time.deltaTime * movementspeed; 
      var targetRotation = Quaternion.LookRotation(baseTarget.position - transform.position); 
      var str = Mathf.Min(.5f * Time.deltaTime, 1); 
      transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str); 
     } 

     if (landed == true) 
      TakeOff(); 

     if (Input.GetKey(KeyCode.L)) 
     { 
      landing = true; 
      lastPosition = transform.position; 
     } 
    } 

    void OnTriggerEnter(Collider other) 
    { 
     if (landing == true && other.gameObject.name == "Base") 
     { 
      StartCoroutine(Landed()); 
     } 
    } 

    bool landed = false; 
    IEnumerator Landed() 
    { 
     yield return new WaitForSeconds(5); 
     Debug.Log("Landed"); 
     landed = true; 
    } 

    private void TakeOff() 
    { 
     if (transform.position != originPosition) 
     { 
      _rigidbody.AddForce(transform.up * 10); 
     } 

     if ((transform.position - originPosition).sqrMagnitude <= (1f * 1f)) 
     { 
      landed = false; 
      _rigidbody.useGravity = false; 
     } 
    } 

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

這是着陸的部分,應該是登陸的部分:

transform.position += transform.forward * Time.deltaTime * movementspeed; 
var targetRotation = Quaternion.LookRotation(baseTarget.position - transform.position); 
var str = Mathf.Min(.5f * Time.deltaTime, 1); 
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, str); 

飛船有兩個組成部分:剛體,利用重力設置爲true。和一個箱子對撞機。

該基地有一個箱對撞機組件。

回答

0

您的車輛上似乎有一個箱式撞擊器,當您的代碼嘗試着陸時,它看起來好像與地形發生碰撞。嘗試將碰撞器切換爲觸發器(碰撞器組件上的勾選選項)。

然後再試一次,因爲這不會導致物理衝突。如果它的工作原因或失敗的原因完全不同,你就知道這是原因或影響因素。

編輯:值得一提的是,當試圖實現這種效果時,觸發動畫比嘗試在基於物理的代碼中實現動畫更容易。 Unity提供了一些非常棒的動畫控制器,您可以在需要着陸時調用動畫,因爲無論如何您都可以遠離玩家。

在這樣做的時候,您可以關閉對撞機,這樣您就不會碰到任何奇怪的碰撞,然後在船需要起飛時將其打開,前提是您需要這樣做。

希望它有幫助。