我在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。我认为您正在使用2种不同的方式同时创建这样的游戏。物理方式和“伪造方式”。即您可以使用铰链关节来遵循物理方法,而不保存任何位置和旋转,也可以伪造它并使用动画进行旋转和移动并记录位置。
我感谢为您的问题压缩脚本的工作,但这只会使事情变得困难。请特别在访问“ SegmentScript”之类的其他脚本时使用注释,并解释为什么要执行自己的操作,并请包括Move函数。