首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么它不每隔300毫秒从名单上发送和移动每架随机无人机?

为什么它不每隔300毫秒从名单上发送和移动每架随机无人机?
EN

Stack Overflow用户
提问于 2021-11-06 06:44:26
回答 1查看 26关注 0票数 0

管理器脚本附加到空的游戏对象:我试图在协同线中使用一个循环,但它只移动了一个无人机,其余的都没有移动。

代码语言:javascript
运行
复制
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附在每个无人机上:

代码语言:javascript
运行
复制
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;
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-06 07:31:51

你的代码几乎没问题。

问题是您使用了WaitForSeconds(300)

300表示300秒,即5分钟

如果你想移动一架无人驾驶飞机,每300毫秒使用WaitForSeconds(0.3f)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69861988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档