管理器腳本附加到空游戲物件:我嘗試在協程中使用回圈,但它只移動了一架無人機,其余的則沒有移動。
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;
}
}
}
uj5u.com熱心網友回復:
你的代碼幾乎沒問題。
問題是你使用WaitForSeconds(300).
300 表示 300 秒,即5 分鐘。
如果您想每 300 毫秒移動一次無人機,請使用 WaitForSeconds(0.3f)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351166.html
下一篇:輪到您啟用和禁用按鈕
