I'm working on a Super Hexagon-like game and the goal is to survive as long as possible. The game is working fine but I want to add a new feature that increases the spawn rate of the obstacles and also beside that, I want to increase the player speed. For example, let's say I want to change the spawn rate to 2 if the player survives 1 minute or change the player speed to 610 after lasting 50 seconds. But the point is that the player should play that 1 minute (or 50 seconds) without dying in order for values to be changed and if he dies in that time, then the values won't change and the timer will be reset. I don't know how I can implement this but I guess I should use coroutines. Any help would be appreciated.
Player script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
public float moveSpeed = 600f;
float movement = 0f;
void Update()
{
movement = Input.GetAxisRaw("Horizontal");
}
void FixedUpdate()
{
transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * -moveSpeed);
}
private void OnTriggerEnter2D(Collider2D other)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
SoundManager.playSound();
}
}
Spawner script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public float spawnRate = 1f;
public GameObject hexagonPrefab;
private float nextTimeToSpawn = 0f;
// Update is called once per frame
void Update()
{
if (Time.time >= nextTimeToSpawn)
{
Instantiate(hexagonPrefab, Vector3.zero, Quaternion.identity);
nextTimeToSpawn = Time.time + 1f / spawnRate;
}
}
}
The game looks like this. When the spawn rate increases, the obstacles duplicate faster.