我在试着写一个产卵控制器脚本。从根本上说,我还想看看是否有更好的方法来做到这一点?
每x秒,SpawnController.cs从一系列数组中随机选择一个单元、开始、PitStop和最终位置。
它用这些变量调用'SpawnSingle.cs‘。
“SpawnSingle.cs”实例化GameObject,在创建时将目标发送到附加到GameObject的“navMove”脚本,等待它到达时等待x秒并更改目标。
问题:
然后SpawnSingle.cs分配第一个停止(pitStop)并告诉它使用navAgents移动到那里。
问题:当它到达PitStop位置时,我希望它等待几秒钟,然后继续到最终目的地。
对于没有Controller的单个实例,这一切都是正常的。
// 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;
}
}发布于 2016-06-14 05:54:58
我认为解决这一问题的最好方法是为每一种行为制定单独的组成部分。
现在你的产卵者负责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 )。如果我误解了你想做的事,请告诉我!
https://stackoverflow.com/questions/37803059
复制相似问题