[英]Jittering motion in 3d snake game using lists in c# with unity3D
我的 3D 蛇克隆出現抖動。
這是代碼:
//movement
private Rigidbody rb;
public float force;
public float turnspeed;
//spawning
public int spawndelay;
public GameObject segment;
private List<Vector3> lastpos = new List<Vector3>();
private List<Quaternion> lastrotation = new List<Quaternion>();
private List<GameObject> segments = new List<GameObject>();
private int i = 1;
private void Start()
{
rb = GetComponent<Rigidbody>();
StartCoroutine(spawnseg());
}
private void Update()
{
lastpos.Insert(0, transform.position);
lastrotation.Insert(0, transform.rotation);
Move();
UpdateSegments();
}
//removed move function(uses rb.addforce and rb.addrelativetorque
private void FixedUpdate()
{
rb.velocity = (force * transform.forward * Time.deltaTime);
}
public void addnewSegment()
{
GameObject newsegment = Instantiate(segment, lastpos[spawndelay * i], lastrotation[spawndelay * i]);
segments.Add(newsegment);
newsegment.GetComponent<SegmentScript>().spawnint = spawndelay * i;
i++;
}
void UpdateSegments()
{
foreach(GameObject intsegment in segments)
{
intsegment.transform.position = lastpos[intsegment.GetComponent<SegmentScript>().spawnint];
intsegment.transform.rotation = lastrotation[intsegment.GetComponent<SegmentScript>().spawnint];
}
}
private IEnumerator spawnseg()
{
yield return new WaitForSeconds(3f);
addnewSegment();
StartCoroutine(spawnseg());
}
基本上我將所有最后的位置保存在列表中,然后讓對象移動到列表中的下一個位置。 然而,即使直行,這些部分似乎也在它們的位置上抖動。
謝謝
我的猜測是你記錄你當前的 position,在Move();
然后將UpdateSegemnts(){}
中的 position 設置回保存的 position。 這需要時間,並且可以解釋抖動。
老實說,到目前為止,很難用你提供的東西給你一個明確的答案。
但作為開始,請不要從協程內部調用協程。 使用 InvokeRepeating。
我認為您正在使用 2 條不同的路徑同時創建這樣的游戲。 物理方式和“假方式”。 IE,您可以使用物理方式將鉸鏈連接到 go 並且不保存任何位置和旋轉,或者您可以偽造它並使用動畫進行旋轉和移動並記錄位置。
我感謝為您的問題壓縮腳本的努力,但這只會讓事情變得困難。 請使用注釋,尤其是在訪問像<SegmentScript>
這樣的另一個腳本時,並解釋您為什么要這樣做,並請包括 Move function。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.