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



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

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

感谢

我的猜测是,您记录当前位置,在Move();中添加一个力,然后将UpdateSegemnts(){}中的位置设置回保存的位置。这需要时间,也许可以解释抖动的原因。

老实说,用你迄今为止提供的东西给你一个明确的答案有点困难。

但作为一个开始,请不要从一个推论内部调用一个推论。使用InvokeRepeating。

我认为你正在使用两种不同的路径来同时创建这样的游戏。物理方式和"伪方式"。也就是说,你可以使用铰链关节来遵循物理方式,而不保存任何位置和旋转,或者你可以伪造它,使用动画进行旋转和移动,并记录位置。

我很感激为你的问题压缩脚本所做的努力,但这只会让事情变得困难。请使用注释,尤其是在访问另一个脚本(如<SegmentScript>(时,并解释您为什么要做您正在做的事情,请包括Move函数。

最新更新