Hello I’m new in unity and c#, I have a big problem when I create my first project create 2D racing game. My problem is I want to increase the difficult every 30 seconds. so every 30 seconds enemy car spawning more faster. I’m stuck here.
Here’s my scripts :
using UnityEngine;
using System.Collections;
public class EnemySpawner : MonoBehaviour
{
public GameObject[] EnemyCars;
int EnemyCarNo;
float maxSpawnRateInSeconds = 4f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
//Function to spawn an enemy
void SpawnEnemy()
{
//this is the bottom-left of the screen
Vector2 min = Camera.main.ViewportToWorldPoint (new Vector2 (0, 0));
//this is the top-right point of the scren
Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1));
//Instantiate an enemy
GameObject anEnemy = (GameObject)Instantiate (EnemyCars[EnemyCarNo]);
anEnemy.transform.position = new Vector2 (Random.Range (min.x, max.x), max.y);
EnemyCarNo = Random.Range (0, 7);
//Schedule when to spawn next enemy
ScheduleNextEnemySpawn ();
}
void ScheduleNextEnemySpawn ()
{
float spawnInNSeconds;
if (maxSpawnRateInSeconds > 1f) {
//pick a number between 1 and maxSpawnRateInSeconds
spawnInNSeconds = Random.Range (1f, maxSpawnRateInSeconds);
} else
spawnInNSeconds = 1f;
Invoke ("SpawnEnemy", spawnInNSeconds);
}
void IncreaseSpawnRate()
{
if (maxSpawnRateInSeconds > 1f)
maxSpawnRateInSeconds--;
if (maxSpawnRateInSeconds == 1f)
CancelInvoke ("IncreaseSpawnRate");
}
//Function to start enemy spawner
public void ScheduleEnemySpawner()
{
Invoke ("SpawnEnemy", maxSpawnRateInSeconds);
//Increase spawn rate every 30 seconds
InvokeRepeating ("IncreaseSpawnRate", 0f, 0.2f);
}
//Function to stop enemy spawner
public void UnscheduleEnemySpawner()
{
CancelInvoke("SpawnEnemy");
CancelInvoke("IncreaseSpawnRate");
}
}
Sorry for my English