So i have just made a spawning script that spawns enemies just outside the visible screen. The method i used under void Update () seems so overcomplicated and hacky though. The problem with this is that the x or the y viewport coordinate needs to be either -0.05 or 1.05 (for x) and -0.06 or 1.06 (for y) which made me use yolorolls, making the code too long. I used decimals so that the enemies would spawn just a bit outside the screen.
Here is the code by the way (Just ignore everything outside the update function).
using UnityEngine;
using System.Collections;
public class SwordsmanSpawn1 : MonoBehaviour {
public GameObject Swordsman;
public int ZombieCount;
public float SpawnwaitMin;
public float SpawnwaitMax;
public int BuyTime;
public Vector3 CameraViewportCoordinates;
public Vector3 CameraViewportCoordinates2;
public Vector3 WorldSpawnCoordinates;
public int YoloRoll;
public int YoloRoll2;
public int YoloRoll3;
private float y;
private float x;
void Start () {
StartCoroutine (SpawnZombies ());
}
void Update () {
YoloRoll = Random.Range (0, 2);
YoloRoll2 = Random.Range (0, 2);
YoloRoll3 = Random.Range (0, 2);
if (YoloRoll == 0)
{
y = -0.06f;
}
else
{
y = 1.06f;
}
if (YoloRoll2 == 0)
{
x = -0.05f;
}
else
{
x = 1.05f;
}
if (YoloRoll3 == 0)
{
WorldSpawnCoordinates = Camera.main.ViewportToWorldPoint (CameraViewportCoordinates);
}
else
{
WorldSpawnCoordinates = Camera.main.ViewportToWorldPoint (CameraViewportCoordinates2);
}
CameraViewportCoordinates = new Vector2 (Random.Range (-0.05f, 1.05f),y);
CameraViewportCoordinates2 = new Vector2 (x, Random.Range (-0.06f, 1.06f));
}
IEnumerator SpawnZombies()
{
yield return new WaitForSeconds (BuyTime);
for (int i = 0; i < ZombieCount; i++)
{
Instantiate(Swordsman, WorldSpawnCoordinates, transform.rotation);
yield return new WaitForSeconds (Random.Range (SpawnwaitMin,SpawnwaitMax));
}
}
}
Any thoughts on how to shorten the update function?
Thanks in advance!
//Taegos