Okay, so I’m attempting to make a 2-Lane scene within my Tower Defense game, I’ve set up 2 lots of Waypoints, each with their own script. I’m thinking of also 3-Lane and 4-Lane versions too… But so far I have come into a rather annoying snag,
Waypoint
using UnityEngine;
public class Waypoints : MonoBehaviour
{
public static Transform[] points;
void Awake ()
{
points = new Transform[transform.childCount];
for (int o = 0; o < points.Length; o++)
{
points[o] = transform.GetChild (o);
}
}
}
Waypoint_2ndLane
using UnityEngine;
public class Waypoints_2ndLane : MonoBehaviour
{
public static Transform[] points2;
void Awake ()
{
points2 = new Transform[transform.childCount];
for (int i = 0; i < points2.Length; i++)
{
points2[i] = transform.GetChild (i);
}
}
}
Plus I have also created a 2nd Lane EnemyMovement script too, so I have 2 seperate enemy prefabs, one with ‘EnemyMovement’ and one with ‘EnemyMovement 2Lane’.
EnemyMovement
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Enemy))]
public class EnemyMovement : MonoBehaviour {
private Transform target;
private int wavepointIndex = 0;
private Enemy enemy;
void Start ()
{
enemy = GetComponent<Enemy> ();
target = Waypoints.points[0];
}
void Update ()
{
Vector3 dir = target.position - transform.position;
transform.Translate(dir.normalized * enemy.speed * Time.deltaTime, Space.World);
if (Vector3.Distance (transform.position, target.position) <= 0.2f)
{
GetNextWaypoint ();
}
enemy.speed = enemy.startSpeed;
}
void GetNextWaypoint()
{
if (wavepointIndex >= Waypoints.points.Length - 1)
{
EndPath ();
return;
}
wavepointIndex++;
target = Waypoints.points [wavepointIndex];
}
void EndPath()
{
PlayerStats.Lives--;
WaveSpawner.EnemiesAlive--;
Destroy (gameObject);
if (WaveSpawner.EnemiesAlive == 0) {
PlayerStats.Money += 10;
}
}
}
EnemyMovement_2Lane
using UnityEngine;
using System.Collections;
public class EnemyMovement_2Lane : MonoBehaviour {
private Transform target2;
private int wavepointIndex = 0;
private Enemy enemy2;
void Start ()
{
enemy2 = GetComponent<Enemy> ();
target2 = Waypoints_2ndLane.points2[0];
}
void Update ()
{
Vector3 dir = target2.position - transform.position;
transform.Translate (dir.normalized * enemy2.speed * Time.deltaTime, Space.World);
if (Vector3.Distance (transform.position, target2.position) <= 0.2f)
{
GetNextWaypoint2 ();
}
enemy2.speed = enemy2.startSpeed;
}
void GetNextWaypoint2()
{
if (wavepointIndex >= Waypoints_2ndLane.points2.Length - 1)
{
EndPath ();
return;
}
wavepointIndex++;
target2 = Waypoints_2ndLane.points2 [wavepointIndex];
}
void EndPath()
{
PlayerStats.Lives--;
WaveSpawner.EnemiesAlive--;
Destroy (gameObject);
if (WaveSpawner.EnemiesAlive == 0) {
PlayerStats.Money += 10;
}
}
}
The final bit of coding is the spawning of the waves, I’ve set the 2 spawn points, and they spawn there fine… So I don’t suspect that it’s the spawner causing this…
WaveSpawner
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class WaveSpawner : MonoBehaviour {
public static int EnemiesAlive = 0;
public Wave[] waves;
public Transform[] spawnPoint;
public float timeBetweenWaves = 20f;
private float countdown = 10f;
private int waveIndex = 0;
public Text waveCountdownText;
void Start ()
{
EnemiesAlive = 0;
}
void Update()
{
if (EnemiesAlive > 0)
{
return;
}
if (countdown <= 0f)
{
StartCoroutine (SpawnWave());
countdown = timeBetweenWaves;
return;
}
countdown -= Time.deltaTime;
countdown = Mathf.Clamp (countdown, 0f, Mathf.Infinity);
waveCountdownText.text = string.Format("Wave " + (waveIndex + 1) + " in {00:00}", countdown);
}
IEnumerator SpawnWave()
{
PlayerStats.Rounds++;
Wave wave = waves [waveIndex];
for (int i = 0; i < wave.count; i++)
{
SpawnEnemy(wave.enemy);
SpawnEnemy2(wave.enemy);
yield return new WaitForSeconds(1f / wave.rate);
}
waveIndex++;
if (waveIndex == waves.Length) {
Debug.Log ("Level Won!");
this.enabled = false;
}
}
void SpawnEnemy(GameObject enemy)
{
Instantiate (enemy, spawnPoint [0].position, spawnPoint [0].rotation);
EnemiesAlive++;
}
void SpawnEnemy2(GameObject enemy)
{
Instantiate (enemy, spawnPoint [1].position, spawnPoint [1].rotation);
EnemiesAlive++;
}
}
The final result is this…
Hopefully someone may be able to assist me on this…