我尝试在Unity中按特定顺序将一个物体传送到不同的位置,但系统没有通过嵌套循环到达下一个传送位置。我在2d数组中预先分配了传送位置,希望系统通过嵌套循环遍历2d数组并相应地移动物体。但它只是将对象移动到SP3位置,而不考虑track_teleporation_index中的元素。所以我假设嵌套的for循环中的if-else if语句有错误,但作为编码和Unity的初学者,经过几天的故障排除,我还是想不出解决办法。
任何帮助或领导将不胜感激!
她就是我的密码:
public class Teleporting : MonoBehaviour
{
public GameObject mouse;
public Transform SP1; //Starting Position in Track 1
public Transform SP2; //Starting Position in Track 2
public Transform SP3; //Starting Position in Track 3
private int[,] track_teleportation_index;
void OnTriggerEnter(Collider other)
{
mouse = GameObject.Find("mouse");
track_teleportation_index = new int[3, 3] { { 1,2,3 }, { 3, 1, 2 }, { 1, 2, 3 } };
for (int row = 0; row < track_teleportation_index.GetLength(0); row++)
for (int col = 0; col < track_teleportation_index.GetLength(1); col++)
{
if (track_teleportation_index[row, col] == 1)
{
mouse.transform.position = SP1.transform.position;
}
else if (track_teleportation_index[row, col] == 2)
{
mouse.transform.position = SP2.transform.position;
}
else if (track_teleportation_index[row, col] == 3)
{
mouse.transform.position = SP3.transform.position;
}
Debug.Log("Teleported to track #" + track_teleportation_index[row, col]);
}
}
}
1条答案
按热度按时间slmsl1lt1#
如果我没理解错的话
一个计数器就足够了