如何使一个对象跟随Unity3D中另一个对象的路径?

时间:2016-10-09 21:51:45

标签: c# unity3d rigid-bodies pathing

我已经尝试在Unity答案网站上提出这个问题,但由于我还没有收到答案,我想我也会在这里提出这个问题。我正在尝试制作游戏“蛇”的3D版本,但我遇到了严重的问题,试图使第一个蛇段跟随蛇的头部。我使用的GameObjects是具有刚体组件的球体,玩家只能控制蛇的“头部”。然后,当蛇长大时,会添加更多的球体,这些球体应该遵循主球体的路径。

我想知道是否有一个更优雅的解决方案,我想要实现的目标,或者至少对我可能做的一些帮助来解决当前的实现。我附加了应该跟随头部的段的代码。对象的命名是西班牙语,但从非西班牙语的人的角度来看,可能并不难以弄清楚发生了什么。

我也正确地评论了代码,这样你就可以理解我在每个句子里做了什么。现在部分起作用的主要思想是向蛇段发送关于头部转弯的确切位置的信息,以便当段到达特定转折点时,它们可以在头部转向的方向上转弯。我遇到的问题是,有时段会经过它应该转弯的点,而我不明白是否是因为精度问题(我正在进行浮点比较以确定段是否到达某个位置它转向的地方)或者它是否是别的东西。

 using UnityEngine;
 using System.Collections.Generic;
 using Clases;

 public class ControladorSegmento : MonoBehaviour {

     //This is a generic list of Objects that store both turning position and turning direction that the head has made
     public List<PosicionCambioDireccion> listaPosicionesCambioDireccion;

     //This would be the head in the case of only one segment that is following
     public GameObject lider;

     //Speed
     public float rapidez;

     //Direction
     public Vector3 direccion;

     //This is an index that is used to iterate over the list of turning points
     private int indiceCambioDireccion;

     // Use this for initialization
     void Start () {

         indiceCambioDireccion = 0;

         listaPosicionesCambioDireccion = new List<PosicionCambioDireccion>();

         //First find the Head of the snake so that we can access its position in order to determine segment spawning position
         lider = GameObject.Find("Cabeza");

         //Set the position of the new segment 2 units right behind the head
         transform.position = lider.GetComponent<Transform>().position - lider.GetComponent<ControladorCabeza>().direccion * 2f;

         //Get the current direction of the head so that the segment inmediately moves in its direction
         direccion = lider.GetComponent<ControladorCabeza>().direccion;
     }

     void LateUpdate () {

         //Check if there has been a change in direction that the segment has to follow
         if ((listaPosicionesCambioDireccion.Count > 0) && (listaPosicionesCambioDireccion.Count > indiceCambioDireccion)) {

             //Compare how close we are to the turning position. If we are sufficiently close, change segment direction
             if (Mathf.Abs(transform.position.x - listaPosicionesCambioDireccion[indiceCambioDireccion].posicion.x) < 0.0999999 &&
                 Mathf.Abs(transform.position.z - listaPosicionesCambioDireccion[indiceCambioDireccion].posicion.z) < 0.0999999) {

                 //Change segment direction at the current position
                 direccion = listaPosicionesCambioDireccion[indiceCambioDireccion].direccion;

                 //Increment turning positions list index so that we get the next turning point in the list (if there is any)
                 indiceCambioDireccion++;
             }
         }
     }

     void FixedUpdate() {


         //Change the velocity
         GetComponent<Rigidbody>().velocity = direccion * rapidez;

     }
 }

我仍然非常认真地使用Unity,似乎我还有很多需要学习的东西。再说一次,如果您对我想要达到的目标有更优雅的解决方案,请告诉我。正确的知道我知道这种实现可能会导致一种内存泄漏,只要头部不断改变方向,存储的转折点列表将继续增长和增长,这显然应该是应该的。避免。

1 个答案:

答案 0 :(得分:0)

您可以在列表(包括头部)的最后一帧中记录每个片段的位置,在下一帧,创建从头部片段的当前位置到最后一帧记录的最后位置的矢量。然后逐步增加头部和头部之和的距离。第一段从头部当前位置开始的半径,并将第一段放在那里。重复这些步骤,所有其他段包括你的蛇。希望这可以帮到你。