using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class DronesManager : MonoBehaviour
{
private List drones = new List();
// Start is called before the first frame update
void Start()
{
drones = GameObject.FindGameObjectsWithTag("Drone").ToList();
StartCoroutine(MoveDrone());
}
// Update is called once per frame
void Update()
{
}
private IEnumerator MoveDrone()
{
for (int i = 0; i < drones.Count; i++)
{
var drone = drones[Random.Range(0, drones.Count)];
if (drone.GetComponent().go == false)
{
drone.GetComponent().movingSpeed = 0.5f;
drone.GetComponent().go = true;
}
yield return new WaitForSeconds(0.3f);
}
}
}
< /code>
Я зацикливаю список беспилотников в коровах, но каждый раз, когда некоторые из беспилотников не остаются двигаться. Пока все беспилотники не будут двигаться.using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DroneControl : MonoBehaviour
{
public float movingSpeed;
public bool go = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(go)
{
transform.position -= transform.forward * movingSpeed * Time.deltaTime;
}
}
}
< /code>
Это то, что я попробовал: < /p>
Все еще некоторые беспилотники остаются и не движутся. < /p>
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class DronesManager : MonoBehaviour
{
private List drones = new List();
private static System.Random rnd = new System.Random();
// Start is called before the first frame update
void Start()
{
drones = GameObject.FindGameObjectsWithTag("Drone").ToList();
StartCoroutine(MoveDrone());
}
// Update is called once per frame
void Update()
{
}
private IEnumerator MoveDrone()
{
for (int i = 0; i < drones.Count; i++)
{
var numbers = new List(Enumerable.Range(0, drones.Count));
var droneIndices = numbers.OrderBy(a => rnd.Next()).ToList();
var drone = drones[droneIndices];
if (drone.GetComponent().go == false)
{
drone.GetComponent().movingSpeed = 0.5f;
drone.GetComponent().go = true;
}
yield return new WaitForSeconds(0.3f);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/698 ... not-moving