0
\$\begingroup\$

I recently made a replica of (Dino run) game in Unity2d.

But I'm stuck on how can I speed up my game gradually.
Should the game speed be increased or should I increase the speed of the camera.

Here is my spawn obstacles script.

public class SpawnObstacles : Monobehaviour

{
    public GameObject [] obstacles;
    private GameObject obj;
    private float XPos = 0;
    private int number = 0;

    private float randomPosition = 0;

    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("Spawn", 1.5f, 1.6f);
    }

   private void Spawn()
    {
        if (GameObject.Find ("Cube").GetComponent<Cube> ().isDead == false)
        {
            number = Random.Range(0, 12);
            randomPosition = Random.Range(19, 29);

            XPos = transform.position.x + randomPosition;
            obj = obstacles[number] as GameObject;

            Instantiate(obj, new Vector2(XPos, obj.transform.position.y), Quaternion.identity);

        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Thank you sir the progression controller idea worked for me \$\endgroup\$
    – user70027
    Commented Jan 17, 2020 at 13:20
  • \$\begingroup\$ When my answer solved your problem, please accept it by clicking the checkmark-icon next to it. \$\endgroup\$
    – Philipp
    Commented Jan 17, 2020 at 18:50

2 Answers 2

2
\$\begingroup\$

A very simple way to control the speed of a Unity game is by changing the value of Time.timeScale. The default value is 1, and higher values mean higher speed. 2 means twice as fast, 0.5 means half as fast, etc.. But keep in mind that this speeds up everything in your game. Often there are various things you do not want to speed up while the game progresses.

Often you want to control the speed of specific game features separately, and some not at all. In that case you have to find different solutions for different kinds of features. You posted the script which spawns objects, so I assume that one thing you might want to increase is the spawn frequency of objects. If that's your goal, then InvokeRepeating is the wrong tool to use, because you can't change the repetition frequency retroactively. Use an update-method instead. Here is a simple script which spawns objects in regular intervals based on Update.

public class RepeatingSpawner : MonoBehaviour {

     public float spawnsPerSecond;
     public GameObject prefab;

     private float nextSpawn;

     void OnEnable() {
         nextSpawn = Time.time;
     }

     void Update() {
         while (nextSpawn < Time.time) {
             nextSpawn += 1.0f / spawnsPerSecond;
             Instantiate(prefab, transform.position, transform.rotation);
         }
     }
}

You can now control the speed by changing the value of "Spawns Per Second". You could do this with an InvokeRepeating coroutine, but there are better ways to do that.

You likely want to change more things than just the speed of this spawner over the course of the game. So it is a good idea to control all of these from one central location. You could create a script with a name like ProgressionController which references everything in the game with a public variable which is supposed to change over the course of the game. But an even more elegant way to handle this could be to use a Timeline asset. It allows you to control the change of multiple values over time using a visual curve editor.

\$\endgroup\$
1
  • \$\begingroup\$ So sorry for that \$\endgroup\$
    – user70027
    Commented Jan 17, 2020 at 18:56
0
\$\begingroup\$

On a language independent level, you can often get this effect by controlling speed of change (change per frame). Pseudo-code being something like:

def time_dialation = 1.0
time_dialation = get_new_time_dialation()
change_in_property = change_in_property * time_dialation
property += change_in_property

For all changes in properties influenced by time (position, rotation, whatever).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .