c# - UNITY 中随机传送位置的嵌套循环

问题描述 投票:0回答:2

我试图在 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]);
            }
    }

}
c# unity3d if-statement nested-loops
2个回答
1
投票

如果我没理解错你想要的是

  • 第一次按下扳机 -> 转到 SP1
  • 第二次按下扳机 -> 转到 SP2
  • 第三次按下扳机 -> 转到 SP3
  • 第四次按下扳机 -> 转到 SP1

一个计数器就足够了

// Simply reference as many target Transform as you need
public Transform[] SPS;

// index for the NEXT target
private int index;

void OnTriggerEnter(Collider other)
{
    // is the mouse not the same object that collides? 
    // in that case you would rather simply do
    //mouse = other.gameObject;
    mouse = GameObject.Find("mouse");

    // teleport to currently next target
    mouse.transform.position = SPS[index].position;

    Debug.Log("Teleported to track #" + SPS[index], SPS[index]);
    
    // Update to the next index, wrapping around at the end of array
    index = (index + 1) % SPS.Length;
}

0
投票

以下是我将如何遍历二维数组中的每个位置,每次

OnTriggerEnter
运行一次。此示例将更改您的很多代码,但将具有所需的功能。我将尝试解释它在做什么,以及它是如何工作的。

public class Teleporting : MonoBehaviour
{
    public GameObject mouse;

    /*
    * If you want just the three starting points, you would need to do
    * this where ever you are setting them now.
    *
    * StartingPoints.Add(StartingPointOne);
    * StartingPoints.Add(StartingPointTwo);
    * StartingPoints.Add(StartingPointThree);
    * 
    * Using a list makes it so you can add as many starting points as 
    * you would like, as well as cleans up the rest of the code a bit
    */
    public List<Transform> StartingPoints { get; set; } = new List<Transform>();
    private int[,] track_teleportation_index;

    // Don't mind the underscore. It's something I picked up from the Microsoft documentation. 
    // It doesn't change functionality
    private int _row; 
    private int _column;

    public Teleporting()
    {
        //Set these to zero when instantiating the class
        _row = 0; 
        _column = 0;
        
        //Set this in here to not make a new one every time the method is called
        track_teleportation_index = new int[3, 3] { { 1,2,3 }, { 3, 1, 2 }, { 1, 2, 3 } }; 
    }

    void OnTriggerEnter(Collider other)
    {
        mouse = GameObject.Find("mouse");

        // This code will move to the next position in your 2d array, 
        // regardless of the size of the array. This will only work
        // for a non null, 2d array.
        _row++;
        
        if (_row > track_teleportation_index.GetLength(0))
        {
            _row = 0;
            _column++;
        }

        if (_column > track_teleportation_index.GetLength(1))
        {
            _column = 0;
        }
        
        // Now get the starting point at the new location
        int desiredStartingPoint = track_teleportation_index[_row,_column];
        
        // Move mouse to the new location. If the desired starting point
        // equals three, then it will be set to the third element added
        // to the list.
        mouse.transform.position = StartingPoints[desiredStartingPoint];

        Debug.Log("Teleported to track #" + desiredStartingPoint);
    }

}

编辑:

这里有一些关于您的评论的澄清信息。

要向

List<Transform>
添加新的起点,在上面的示例中,该列表称为
StartingPoints
,您将使用列表的
.Add()
功能。这允许您向列表中添加一个新的
Transform
。将您的代码与我所做的更改混合使用,这就是我的做法。

public List<Transform> StartingPoints { get; set; } = new List<Transform>();

public Transform SP1; //Starting Position in Track 1
public Transform SP2; //Starting Position in Track 2
public Transform SP3; //Starting Position in Track 3

public Teleporting()
{
    //Set these to zero when instantiating the class
    _row = 0; 
    _column = 0;

    //Set this in here to not make a new one every time the method is called
    track_teleportation_index = new int[3, 3] { { 1,2,3 }, { 3, 1, 2 }, { 1, 2, 3 } }; 

    StartingPoints.Add(SP1);
    StartingPoints.Add(SP2);
    StartingPoints.Add(SP3);
}

这样做将使前三个起点变得可访问。您可以从有权访问

Teleporting
类的任何类向该列表添加更多起点。这是一个可能看起来像的例子:

public class MyClass
{

    public Teleporting teleporting = new Teleporting();
    
    public Transform SP1; //Starting Position in Track 1
    public Transform SP2; //Starting Position in Track 2
    public Transform SP3; //Starting Position in Track 3

    public AddStartingPoints()
    {
        teleporting.StartingPoints.Add(SP1);
        teleporting.StartingPoints.Add(SP2);
        teleporting.StartingPoints.Add(SP3);
    }
}

希望对您有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.