Increasing scrolling speed on runner game stops increasing

I’m new to Unity and currently doing a Unity Learn course. The videos are down right now, so I’m working on the functions of my personal project, but I’m having trouble increasing the scrolling speed. The code I have works for everything except increasing speed. For the first few seconds of the game, the speed increases. However, within a few seconds, the speed remains constant and I don’t even get the Debug.Log information anymore. Currently, I have:

    private float levelWaitTime = 1.0f;
    private float timer = 0.0f;
    private float difficultyIncrease = 0.2f;
    void IncreaseSpeed()
    {
        timer += Time.deltaTime;

        //increase speed over time
        if (timer > levelWaitTime)
        {
            speed += difficultyIncrease;
            Debug.Log("move left speed:" + speed);
            timer = timer - levelWaitTime;
        }
    }

In my update function, I also call two other functions, but as far as I can tell, it shouldn’t get stuck in either other functions. One function moves the objects and the other one destroys them when their out of bounds.

Thanks for any help!

Hmm… can you share the code where you are calling this function from?

Also, do you happen to be changing Time.timeScale at all?

void Update()
    {
        MoveObjectsLeft();
        IncreaseSpeed();
        DestroyOutOfBounds();
    }

That’s pretty much it, and I don’t call Time.timeScale anywhere. In case it helps:

void MoveObjectsLeft()
    {
        if (playerControllerScript.gameOver == false)
        {
            transform.Translate(Vector3.left * Time.deltaTime * speed);
        }
    }

void DestroyOutOfBounds()
    {
        if (transform.position.x < leftBound && !gameObject.CompareTag("Ground"))
        {
            Destroy(gameObject);
        }
        else if (transform.position.z < lowerBound)
        {
            Destroy(gameObject);
        }
    }

Are you touching any of these variables in the other functions? (timer, difficultyIncrease, levelWaitTime)?

I went in and updated my message right before you replied with everything except the variables at the top, but it looks like you were faster than I was. Hopefully my edit helps and thank you again for your time.

1 Like

One final question - is speed a static variable?

Nope, it’s a private float. Should it not be?

private and static are not mutually exclusive.

I’m just trying to get the full picture of what’s going on here. Basically, it seems like this is supposed to be a gradual level difficulty increasing thing - but if speed is just a member variable of this object that goes left until it gets destroyed, then the overall difficulty of the level is not going to go up. Each newly spawned object (I assume you’re Instantiating these somewhere?) will simply start with whatever the normal starting speed is from your prefab.

Question - when you say you are not getting the log anymore - are you sure you’re actually not? Maybe you just have the “Collapse” toggle turned on in your console, so it is grouping the identical logs together?

1 Like

You’re right about the console. I should have thought of that. What I’m seeing now that I’ve unclicked collapse makes sense with what you’re saying.

I have a separate script that spawns items. I’ll try timing the speed in that script and then calling it in this script. Since I already increased the number of spawns over time in the other script, it should work.

Thank you so much!

1 Like