繁体   English   中英

3d 蛇游戏中的抖动运动使用 c# 中的列表和 unity3D

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM