【问题标题】:Jittering motion in 3d snake game using lists in c# with unity3D使用带有unity3D的c#中的列表在3d蛇游戏中抖动运动
【发布时间】:2020-05-02 15:27:25
【问题描述】:

我的 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());
}

基本上我将所有最后的位置保存在列表中,然后让对象移动到列表中的下一个位置。 然而,即使直行,这些路段似乎也在原地抖动。

谢谢

【问题讨论】:

    标签: c# list unity3d 3d


    【解决方案1】:

    我的猜测是,你记录下你当前的位置,在Move(); 中添加一个力,然后将UpdateSegemnts(){} 中的位置设置回保存的位置。这需要时间,并且可能会解释这种抖动。

    说实话,目前为止您提供的内容很难给您一个明确的答案。

    但首先请不要从协程内部调用协程。使用 InvokeRepeating。

    我认为您正在使用 2 条不同的路径同时创建这样的游戏。物理方式和“假方式”。 IE。您可以使用铰链关节以物理方式而不保存任何位置和旋转,或者您可以伪造它并使用动画进行旋转和移动并记录位置。

    我很欣赏为您的问题压缩脚本的努力,但这只会让事情变得困难。请使用 cmets,尤其是在访问像 &lt;SegmentScript&gt; 这样的另一个脚本时,并解释您为什么要做您正在做的事情,并请包含移动功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      相关资源
      最近更新 更多