0
\$\begingroup\$

I'm making a small game similar to Super Hexagon. I've recently asked a question about making the game harder as the player progresses by increasing the obstacles' spawn rate. I was able to implement this feature with the help of an answer. The game works fine in the game view and the spawn rate is low at first and increases slowly which is exactly what I want. But the problem starts where I create a build. Unlike the game view, the spawn rate is high as soon as the game starts but after a few seconds it changes back to what it was in the game view. I changed the values on the Animation Curve multiple times and found out that this difference only occurs with my current values. In fact, if I change the values to something that starts high at the beginning, the game view and the build behave similarly but using values like these and even smaller, creates this issue.

Spawner script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawner : MonoBehaviour
{
    // the spawn rate is 1 in the inspector and it doesn't change by the curve.
    public float spawnRate = 1f;

    public GameObject hexagonPrefab;

    private float nextTimeToSpawn = 0f;
    public AnimationCurve spawnOverTime;
    public LifeTime life;

     void FixedUpdate()
    {
        float spawn = spawnRate * spawnOverTime.Evaluate(life.TimeAlive);
        if (Time.time >= nextTimeToSpawn)
        {
            Instantiate(hexagonPrefab, Vector3.zero, Quaternion.identity);
            nextTimeToSpawn = Time.time + spawn / spawnRate;
        }
       
    }
}

enter image description here The game when it starts in the game view.

enter image description here The game when the build starts.

enter image description here This is the spawner's Animation Curve.
First point from left: time = 11, value = 1
Second point: time = 164.2441 , value = 0.3

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Do you have another scene before this in your build - like a title screen or main menu? What happens if you add a Start() method with nextTimeToSpawn = Time.time;? \$\endgroup\$
    – DMGregory
    Jun 1, 2021 at 1:06
  • \$\begingroup\$ Wow! Adding Start() method solved the problem. Thank you very much! I don't think this matters now but no, I don't have another scene before this one. I'll write the new code as an answer just in case if somebody else has the same problem. \$\endgroup\$
    – Ali
    Jun 1, 2021 at 2:03

1 Answer 1

0
\$\begingroup\$

I updated the code and now it works perfectly fine.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawner : MonoBehaviour
{
    public float spawnRate = 1f;

    public GameObject hexagonPrefab;

    private float nextTimeToSpawn = 0f;
    public AnimationCurve spawnOverTime;
    public LifeTime life;
    void Start()
    {
        nextTimeToSpawn = Time.time;
    }

     void FixedUpdate()
    {
        float spawn = spawnRate * spawnOverTime.Evaluate(life.TimeAlive);
        if (Time.time >= nextTimeToSpawn)
        {
            Instantiate(hexagonPrefab, Vector3.zero, Quaternion.identity);
            nextTimeToSpawn = Time.time + spawn / spawnRate;
        }
       
    }
}
\$\endgroup\$
5
  • 2
    \$\begingroup\$ The trick here is that Time.time starts counting before your gameplay starts. So by the time you get your first FixedUpdate, TIme.time already has some time buffered, and your spawner spawns multiple times back-to-back to try to catch up to it. By setting your spawn time in Start, you ignore any time that was accumulated before Start had a chance to run. \$\endgroup\$
    – DMGregory
    Jun 1, 2021 at 2:09
  • \$\begingroup\$ Thanks for the explanation. The only question I have is that why doesn't that issue happen in game mode? Does Unity count time differently? \$\endgroup\$
    – Ali
    Jun 1, 2021 at 2:14
  • 2
    \$\begingroup\$ No splash screen in-editor? \$\endgroup\$
    – DMGregory
    Jun 1, 2021 at 2:16
  • \$\begingroup\$ Oh I see. That's right. I didn't know that splash screen is also included in time counting process. Thanks! \$\endgroup\$
    – Ali
    Jun 1, 2021 at 2:18
  • 1
    \$\begingroup\$ Don't forget to mark this answer as Accepted if it worked for you. \$\endgroup\$
    – DMGregory
    Jul 1, 2021 at 12:19

You must log in to answer this question.

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