我正在尝试编写一个派生控制器脚本。从根本上说,我还想看看是否有更好的方法来做到这一点?
什么:SpawnController.cs每隔x秒从一系列数组中选择一个随机单元、起始、PitStop和最终位置。
它用这些变量调用"SpawnSingle。cs"。
"SpawnSingle。cs"实例化游戏对象,在创建时将目的地发送到附加到游戏对象的"navMove"脚本一次,当它到达并更改目的地时等待x秒。
问题:
-
如何创建调用脚本(SpawnSingle)的每个实例中途"等待"x秒,因为它正在控制gameObject。我无法使用协同程序来阻止它。
-
之后的第二组坐标如何传递?它没有似乎在使用SpawnController时工作。
//In SpawnController.cs ... ... private Transform currentDestination; private NavMove moveScript; private GameObject newEnemy; public static SpawnSingle newSpawn = new SpawnSingle(); void Start() { // To build a function to randomise the route and spawn at intervals spawnCounter = 0; Spawn(); spawnCounter++; Spawn(); void Spawn() { newSpawn.SpawnEnemy(Enemies[spawnCounter], SpawnPoint[spawnCounter], PitStop[spawnCounter], Destination[spawnCounter]); }
SpawnSingle.cs然后分配第一个停靠点(pitStop),并使用navAgents告诉它移动到那里。
问题:当它到达PitStop位置时,我希望它等待几秒钟,然后继续到达最终目的地。
对于没有控制器的单个实例,这一切都可以正常工作。
// 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;
}
}
我认为解决这个问题的最佳方法是为每个行为制作单独的组件。
现在,您的spawner负责3件事:1) 制造敌人,2)设置初始导航点,3)稍后更新导航点。
我建议让你的产卵者只负责产卵的物体。
然后制作一个导航组件,创建导航点和进站等。
这样的东西:
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)。如果我误解了你的意图,请告诉我!