Make an enemy generator
I'm trying to make an endless game where the player can only move in 3 specific lanes (up, mid, and down) and try to survive the enemies coming from these lanes. The main problem it's i don't know how to make an enemy generator...
This is the player movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
GameObject SpawnCentrale;
GameObject SpawnBasso;
GameObject SpawnAlto;
// Start is called before the first frame update
void Start()
{
SpawnCentrale = GameObject.FindWithTag("Spawn1");
SpawnBasso = GameObject.FindWithTag("Spawn2");
SpawnAlto = GameObject.FindWithTag("Spawn3");
transform.position = GameObject.Find("Spawn1").transform.position;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.W) && transform.position == SpawnCentrale.transform.position)
{
transform.position = GameObject.Find("Spawn3").transform.position;
}
if (Input.GetKeyDown(KeyCode.W) && transform.position == SpawnBasso.transform.position)
{
transform.position = GameObject.Find("Spawn1").transform.position;
}
if (Input.GetKeyDown(KeyCode.S) && transform.position == SpawnCentrale.transform.position)
{
transform.position = GameObject.Find("Spawn2").transform.position;
}
if (Input.GetKeyDown(KeyCode.S) && transform.position == SpawnAlto.transform.position)
{
transform.position = GameObject.Find("Spawn1").transform.position;
}
}
}
This is the enemy movement (for just one lane):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float speed = 5.0f;
private Rigidbody2D rb;
GameObject CancellaNemici;
// Start is called before the first frame update
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(-speed, 0);
CancellaNemici = GameObject.FindWithTag("DeleteEnemy");
}
// Update is called once per frame
void Update()
{
if (transform.position.x < CancellaNemici.transform.position.x){
Destroy(this.gameObject);
}
}
}
And this is my attempt to make an enemy generator, but it's not working:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeployEnemy : MonoBehaviour
{
public GameObject EnemyPrefab;
GameObject EnemySpawn;
void Start()
{
StartCoroutine(SpawnObjects());
EnemySpawn = GameObject.FindWithTag("SpawnEnemy");
}
IEnumerator SpawnObjects()
{
while (true)
{
Instantiate(EnemyPrefab, EnemySpawn.transform.position, Quaternion.identity);
yield return new WaitForSeconds(3.0f);
}
}
}
I'm new to game development and programming in general so i'm really lost >_<
Your answer
Follow this Question
Related Questions
Can't spawn more than one NavMeshAgent wave system 0 Answers
Camera Issue,Camera is changing it's position in game view. 0 Answers
3d working music player 0 Answers
Endless Runner platform Heigh generator not working. 0 Answers
How to Make Game Object (Enemy) Move (Right to Left) in 2D Endless Runner Game? 0 Answers