Question by
Nosmo · Mar 27, 2019 at 08:02 PM ·
followienumeratoroffsetfollow playerstartcoroutine
How can i get my enemy spawner follow the player?
I'm trying to make my enemy spawner follow the player. At the moment its set to 40 on z but when the player moves past that they keep spawning in that 1 place.
here is the code so far:
public class GameController : MonoBehaviour {
public GameObject hazard;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public GameObject player; //New follow code
private Vector3 OffSet; //New follow code
private void Start()
{
StartCoroutine(SpawnWaves());
}
IEnumerator SpawnWaves() {
yield return new WaitForSeconds (startWait);
while (true) //infinate loop
{
for (int i = 0; i < hazardCount; i++)
{
LateUpdate();
/* original spawn in 1 place code
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = new Quaternion();
Instantiate(hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
*/ original code
}
yield return new WaitForSeconds(waveWait);
}
}
// New follow code
void LateUpdate()
{
transform.position = player.transform.position + OffSet; // New follow code
Quaternion spawnRotation = new Quaternion();
Instantiate(hazard, player.transform.position, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
}
I get an error message saying "The body of 'GameController.LateUpdate()' cannot be an iterator block because 'void' is not an iterator interface type"
Does anyone have suggestion?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612194417im_/https://answers.unity.com/themes/thub/images/avi.jpg)