Spawner.cs 1.1 KB
Newer Older
Naliwe GS committed
1 2
using System;
using UnityEngine;
Naliwe GS committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
using System.Collections;

public class Spawner : MonoBehaviour
{
    public GameObject BasicEnemy;
    public GameObject StaticEnemy;
    public GameObject Toluen;

    private float _timer;
    private System.Random _rand;
    private float _rightBorder;

	void Start ()
	{
	    _timer = 0f;
        _rand = new System.Random();
	}
	
	void Update ()
	{
	    _timer += Time.deltaTime;
	    _rightBorder = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0)).x + 2;

Naliwe GS committed
26
	    if (_timer >= Math.Max((2 / (Time.time / 10)), .5f))
Naliwe GS committed
27 28 29 30 31 32 33 34 35 36 37
	    {
	        Spawn();
	        _timer = 0f;
	    }
	}

    private void Spawn()
    {
        var r = _rand.Next(100);

        if (r < 20)
38
            Instantiate(StaticEnemy, new Vector3(_rightBorder, _rand.Next(3, 12), 0), Quaternion.identity);
Naliwe GS committed
39
        else if (r < 45)
Naliwe GS committed
40
            Instantiate(Toluen, new Vector3(_rightBorder, _rand.Next(1, 9), 0), Quaternion.identity);
Naliwe GS committed
41 42
        r = _rand.Next(100);
        if (r < 50)
43
            Instantiate(BasicEnemy, new Vector3(_rightBorder, _rand.Next(1, 12), 0), Quaternion.identity);
Naliwe GS committed
44 45
    }
}