首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >脚本可以生成具有多个navAgent目的地的单元,以及使用Unity脚本的等待时间

脚本可以生成具有多个navAgent目的地的单元,以及使用Unity脚本的等待时间
EN

Stack Overflow用户
提问于 2016-06-14 04:33:46
回答 1查看 131关注 0票数 0

我在试着写一个产卵控制器脚本。从根本上说,我还想看看是否有更好的方法来做到这一点?

每x秒,SpawnController.cs从一系列数组中随机选择一个单元、开始、PitStop和最终位置。

它用这些变量调用'SpawnSingle.cs‘。

“SpawnSingle.cs”实例化GameObject,在创建时将目标发送到附加到GameObject的“navMove”脚本,等待它到达时等待x秒并更改目标。

问题:

  • 如何使被调用的脚本(SpawnSingle)的每个实例在中途等待x秒,因为它正在控制gameObject。我不能用协同线来阻止它。
  • 之后如何传递第二组坐标?在使用SpawnController时,它似乎不起作用。 //在SpawnController.cs ..。私有转换currentDestination;私有NavMove moveScript;私有GameObject newEnemy;公共静态SpawnSingle newSpawn =新SpawnSingle();void Start() { //构建一个函数,将路由随机化,并按间隔spawnCounter =0进行产卵;产卵();spawnCounter++;产卵();空产卵(newSpawn.SpawnEnemy,SpawnPointspawnCounter,PitStopspawnCounter,DestinationspawnCounter);}

然后SpawnSingle.cs分配第一个停止(pitStop)并告诉它使用navAgents移动到那里。

问题:当它到达PitStop位置时,我希望它等待几秒钟,然后继续到最终目的地。

对于没有Controller的单个实例,这一切都是正常的。

代码语言:javascript
复制
 // in SpawnSingle.cs
private Transform currentDestination;
private NavMove moveScript; // This script moves the navAgent
private GameObject newEnemy;
public void SpawnEnemy(GameObject Enemies, Transform SpawnPoint, Transform PitStop, Transform Destination)
{
    newEnemy = GameObject.Instantiate(Enemies, SpawnPoint.position, Quaternion.identity) as GameObject;
    moveScript = newEnemy.GetComponent<NavMove>();
    currentDestination = PitStop;
    moveScript.destination = currentDestination;

    if (arrivedAtP())
    {
    // Stop and wait x seconds
    moveScript.nav.enabled = false;
    // ***HELP HERE*** How do I make this script wait? Coroutines don't work when this script is called from an extenral source it seems?
    // Wait for x seconds--


    //Continue moving to final destination
    //*** HELP HERE*** When instantiated from an external script, this doesn't continue to pass in the new location?***
    moveScript.nav.enabled = true;
    currentDestination = Destination;
    moveScript.destination = currentDestination;

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-14 05:54:58

我认为解决这一问题的最好方法是为每一种行为制定单独的组成部分。

现在你的产卵者负责3件事: 1)制造敌人,2)设置初始导航点,3)更新导航点。

我建议你的产卵者只对产卵物负责。

然后制作一个导航组件,创建导航点和站台等。

所以就像这样:

代码语言:javascript
复制
public class Spawner : MonoBehaviour {
    //only spawns, attached to some gameobject
    public GameObject prefabToSpawn;

    public GameObject Spawn() {
        //instantiate etc..
        GameObject newObject = Instantiate(prefabToSpawn);
        return newObject
    }

}

public class EnemyManager : MonoBehaviour {
    //attached to an empty gameObject
    public Spawner spawner;

    public Enemy CreateNewEnemy () {
        GameObject newEnemy = spawner.Spawn ();
        // add it to list of enemies or something
        //other stuff to do with managing enemies

    }



}

public class Navigator : MonoBehaviour {
    //Attached to Enemy prefab

    Destination currentDestination;

    public float changeDestinationTime;


    void Start() {
        currentDestination = //first place you want to go

        InvokeRepeating ("NewDestination", changeDestinationTime, changeDestinationTime);
    }


    void NewDestination() {
        currentDestination = // next place you want to go
    }

}

显然,这不是一个完整的解决方案,但它应该有助于您指出正确的方向(并且每8秒改变方向:D )。如果我误解了你想做的事,请告诉我!

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

https://stackoverflow.com/questions/37803059

复制
相关文章

相似问题

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