2016-10-09 85 views
0

我試過在Unity答案網站上提出這個問題,但由於我還沒有收到答案,我想我也會在這裏問這個問題。我試圖製作一個3D版的遊戲「蛇」,但我遇到了嚴重的問題,試圖讓第一個蛇段跟在蛇頭上。我使用的GameObjects是帶有剛體組件的球體,玩家只能控制蛇的「頭部」。然後,隨着蛇的增長,更多的球體應該沿着主球體的路徑添加。如何在Unity3D中使對象跟隨另一個對象的路徑?

我想知道是否有一個更優雅的解決方案,我想實現什麼,或者至少有一點幫助我可以做什麼來修復當前的實現。我附加了應該跟隨頭部的細分代碼。對象的命名採用西班牙文,但從非西班牙語人士的角度來看,可能並不難以弄清楚發生了什麼事情。

我也正確地評論了代碼,以便您能夠理解我在每個句子中所做的事情。現在部分工作的主要想法是發送關於頭部旋轉到什麼位置的信息,以便當線段到達特定的轉折點時,他們可以在頭部轉向的方向上轉彎。我遇到的問題是,有時段會經過應該轉彎的點,而我不明白是否由於精度問題(我正在進行浮點比較以確定段是否已達到某個位置在哪裏轉)或者如果是別的東西。

using UnityEngine; 
using System.Collections.Generic; 
using Clases; 

public class ControladorSegmento : MonoBehaviour { 

    //This is a generic list of Objects that store both turning position and turning direction that the head has made 
    public List<PosicionCambioDireccion> listaPosicionesCambioDireccion; 

    //This would be the head in the case of only one segment that is following 
    public GameObject lider; 

    //Speed 
    public float rapidez; 

    //Direction 
    public Vector3 direccion; 

    //This is an index that is used to iterate over the list of turning points 
    private int indiceCambioDireccion; 

    // Use this for initialization 
    void Start() { 

     indiceCambioDireccion = 0; 

     listaPosicionesCambioDireccion = new List<PosicionCambioDireccion>(); 

     //First find the Head of the snake so that we can access its position in order to determine segment spawning position 
     lider = GameObject.Find("Cabeza"); 

     //Set the position of the new segment 2 units right behind the head 
     transform.position = lider.GetComponent<Transform>().position - lider.GetComponent<ControladorCabeza>().direccion * 2f; 

     //Get the current direction of the head so that the segment inmediately moves in its direction 
     direccion = lider.GetComponent<ControladorCabeza>().direccion; 
    } 

    void LateUpdate() { 

     //Check if there has been a change in direction that the segment has to follow 
     if ((listaPosicionesCambioDireccion.Count > 0) && (listaPosicionesCambioDireccion.Count > indiceCambioDireccion)) { 

      //Compare how close we are to the turning position. If we are sufficiently close, change segment direction 
      if (Mathf.Abs(transform.position.x - listaPosicionesCambioDireccion[indiceCambioDireccion].posicion.x) < 0.0999999 && 
       Mathf.Abs(transform.position.z - listaPosicionesCambioDireccion[indiceCambioDireccion].posicion.z) < 0.0999999) { 

       //Change segment direction at the current position 
       direccion = listaPosicionesCambioDireccion[indiceCambioDireccion].direccion; 

       //Increment turning positions list index so that we get the next turning point in the list (if there is any) 
       indiceCambioDireccion++; 
      } 
     } 
    } 

    void FixedUpdate() { 


     //Change the velocity 
     GetComponent<Rigidbody>().velocity = direccion * rapidez; 

    } 
} 

我還是一個真正與Unity合作的新人,看起來我還有很多東西需要學習。再說一次,如果你有一個更優雅的解決方案來實現我的目標,請告訴我。我知道我知道這種實現可能會導致一種內存泄漏,只要頭部不斷變化方向,存儲的轉折點列表將會保持增長和增長,這顯然是應該的避免。

回答

0

您可以在列表(包括頭)中的最後一幀中記錄每個段的位置,在下一幀中,從頭段的當前位置到最後一幀記錄的最後位置創建一個向量。然後,從頭部的當前位置開始,將第一個線段的半徑的總和與第一個線段的總和的距離,並將第一個線段放置在那裏。重複這些步驟爲所有其他段組成你的蛇。希望這可以幫到你。

+0

謝謝您的貢獻。我是這麼做的,但是如果我直接控制球體的位置,那麼運動看起來很詭異和醜陋。我正在尋找一種解決方案,通過修改剛體的實際速度來移動球體,因爲移動動畫更加流暢。你推薦的解決方案使它看起來像是被傳送的球體。 – Greg

+0

您可以通過物理速度來移動頭部,但在Update()中更新其他片段會調用每一幀,這將使它們變得流暢。 – ruisen

相關問題