如何使一个对象遵循另一个对象的路径在Unity3D

本文关键字:一个对象 路径 Unity3D 何使 | 更新日期: 2023-09-27 18:12:11

我试着在Unity答案网站上问这个问题,但因为我还没有收到答案,我想我也会在这里问这个问题。我正在尝试制作游戏"Snake"的3D版本,但我遇到了严重的问题,试图使第一个蛇段跟随蛇的头部。我使用的游戏对象是带有刚体组件的球体,玩家只能控制蛇的"头部"。然后,随着蛇的生长,更多的球体被添加,它们应该遵循主球体的路径。

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

我还正确地注释了代码,以便您可以理解我在每句话中所做的事情。目前部分有效的主要想法是向蛇段发送有关头部转弯的确切位置的信息,这样当蛇段到达特定的转折点时,它们就可以朝着头部转弯的方向转弯。我遇到的问题是,有时段经过它应该转弯的点,我不明白这是因为精度问题(我正在进行浮点比较,以确定一个段是否已经达到了它转弯的某个位置),或者如果它是别的什么。

 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的新手,似乎我还有很多东西要学。再说一次,如果你有一个更优雅的解决方案,我正在努力实现,请让我知道。我知道,这种实现可能会导致某种意义上的内存泄漏,只要头部不断改变方向,存储的转折点列表就会不断增长,这显然是应该避免的。

如何使一个对象遵循另一个对象的路径在Unity3D

您可以在列表中记录每个片段在上一帧中的位置(包括头部),在下一帧中,从头部片段的当前位置到上一帧记录的最后位置创建一个向量。然后步进两个头部和的距离&第一段距离头部当前位置的半径,并将第一段放在那里。对蛇的其他部分重复这些步骤。