Search Unity

Question random spawn enemy in random time?

Discussion in 'Getting Started' started by Shiroe0099, Jun 1, 2023.

  1. Shiroe0099

    Shiroe0099

    Joined:
    May 31, 2023
    Posts:
    5
    My idea is that the enemies will randomly appear in the left or right direction and the time will count down from 3 minutes. With this in mind, this level can be replayed multiple times and each time the score will be different depending on how many enemies appear.

    However, after running a test with a "ratespawn" value of 50, I only got a few enemies after about 10 seconds, and then none of them appeared. I don't know if I misunderstood the Random function.

    If anyone has any ideas please help me.

    Thanks everyone.
    upload_2023-6-1_23-52-52.png
     

    Attached Files:

  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    I have a coin and I toss it 100 times. What result can be? For sure it's not 50 heads and 50 tails. The chance is 50:50 but the result does not have to.

    I think you should have some guaranteed spawn in gameplay, ex. one enemy per three seconds and a random extra chance to spawn 0-2 enemies. Thanks to that you have a steady game flow, some rhythm, and random challenges.

    Edit: One thing. There are two Range functions, one requires integers (result is an integer), and the second floats (result is float). So if you want float result type Random.Range(1f, 101f).
     
    Last edited: Jun 1, 2023
    Shiroe0099 likes this.
  3. Shiroe0099

    Shiroe0099

    Joined:
    May 31, 2023
    Posts:
    5
    I understand what you mean. I will try it. Thanks for your opinion.
     
  4. Shiroe0099

    Shiroe0099

    Joined:
    May 31, 2023
    Posts:
    5
    haha, I know what I did wrong.
    nextSpawn += Time.time + spawnRate
    it should be: nextSpawn = Time.time + spawnRate
     
    AngryProgrammer likes this.