Unity .. логический цикл для проверки 12 позиции - PullRequest
0 голосов
/ 09 января 2019

У меня есть этот код:

if(car.position == a1.position)
  {
    car.speed = 0;
    a1 = 1;
   if(cooldow == 0){
        car.taget.position = targetHere.position
        car.speed = 5;
        a1 = 0;
        Destroy(obj, 5)
       }
  }

первое условие [if (car.position == a1.position)] я буду повторять его более 10 раз, то есть я буду проверять, будет ли (car.position) такой же, как a2.position , затем 3.позиция , затем a4.position , затем a5.position , затем a6.position , затем b1.position , затем b2.position , затем b3.position , затем b4.position , затем b5.position , затем b6.position .... двенадцать раз ... и я не смог найти логику, чтобы поместить ее в ( For ) цикл.

Любая идея, как это сделать !!! TY

Ответы [ 3 ]

0 голосов
/ 09 января 2019

Вы можете создать коллекцию своих переменных и запустить через них foreach.

List<CarType> collection = new List<CarType> {a1, b1, a2, b2 ...};
foreach (CarType c in collection)
{
    // do stuff
}
0 голосов
/ 09 января 2019

Я предполагаю, что a1 является преобразованием, поскольку вы обращаетесь к его переменной позиции. Я также предполагаю, что вы назначаете эти игровые объекты индивидуально в инспекторе.

Я бы сделал следующее:

// Make sure to put all your a objects in this list in the inspector
public List<Transform> myABCObjects; 

void CheckPositions()
{
    // This foreach loop, will loop through your list of transforms, so your a b c 
    // objects would be added to this list via the inspector, or through whatver
    // code you are using to set them.
    foreach(Transform a in myABCObjects)
    {
        // Not to sure if you can do an == on a vector3, but I wouldn't want to
        // considering the data members are float, and can cause floating point error
        // Because of this I am getting the distance to the location, and using
        // Mathf.Approximately to check if the distance is close to 0f.
        if(Mathf.Approximately(car.position.distance(a.position), 0f))
        {
            // Your logic here if location is the same...
            // so if you are trying to remove say a1
            // add this object to a list, to be removed after the loop
            // then continue on.
        }
    }

    // if you are removing an object, check the list size, then loop through it to remove the objects from your original list...
}

Пример использования цикла for вместо foreach:

// Make sure to put all your a objects in this list in the inspector
public List<Transform> myABCObjects; 

void CheckPositions()
{
    // This for loop, will loop through indices 0 - myABCObjects.count - 1, so your a b c 
    // objects would be added to this list via the inspector, or through whatver
    // code you are using to set them.
    for(int i = 0; i < myABCObjects.count; ++i)
    {
        // Not to sure if you can do an == on a vector3, but I wouldn't want to
        // considering the data members are float, and can cause floating point error
        // Because of this I am getting the distance to the location, and using
        // Mathf.Approximately to check if the distance is close to 0f.
        if(Mathf.Approximately(car.position.distance(myABCObjects[i].position), 0f))
        {
             // Your logic here if location is the same...

        }
    }

}
0 голосов
/ 09 января 2019

Если [a1..a6] и [b1..b6] имеют одинаковый тип, то попробуйте сделать это:

var list = new List<TypeOfa1> {a1, a2, a3, a4, a5, a6, b1, b2, b3, b4, b5, b6};             

foreach (var item in list)
{
    if (car.position == item.position)
    {
        car.speed = 0;
        item = 1;
        if (cooldow == 0)
        {
            car.taget.position = targetHere.position;
            car.speed = 5;
            item = 0;
            Destroy(obj, 5);
        }
    }
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...