管理器脚本附加到空的游戏对象:我试图在协同线中使用一个循环,但它只移动了一个无人机,其余的都没有移动。
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class DronesManager : MonoBehaviour
{
private List<GameObject> drones = new List<GameObject>();
// 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<DroneControl>().go == false)
{
drone.GetComponent<DroneControl>().movingSpeed = 0.5f;
drone.GetComponent<DroneControl>().go = true;
}
yield return new WaitForSeconds(300);
}
}
}它只移动一架,但在无人机名单上有18架无人机。我想每隔300毫秒移动一次随机无人机。
脚本DroneControl附在每个无人机上:
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;
}
}
}发布于 2021-11-06 07:31:51
你的代码几乎没问题。
问题是您使用了WaitForSeconds(300)。
300表示300秒,即5分钟。
如果你想移动一架无人驾驶飞机,每300毫秒使用WaitForSeconds(0.3f)
https://stackoverflow.com/questions/69861988
复制相似问题