0
\$\begingroup\$

This is code that I wanted to put in my IF statement

    public GameObject fallingRockPrefab;
public Vector2 spawnsMinMax;
float randX;
float randY;

//Spawn Positions
Vector2 SpawnPosition;
float nextSpawnTime;

void Update()
{
    if (Time.time > nextSpawnTime)
    {
        float SpawningInBetween = Mathf.Lerp(spawnsMinMax.y, spawnsMinMax.x, Difficulty.GetDifficultyPercent());
        print(SpawningInBetween);
        nextSpawnTime = Time.time + SpawningInBetween;

        randX = Random.Range(-5.6f, 5.6f);
        randY = 5.57f;
        SpawnPosition = new Vector2(randX, randY);
        Instantiate(fallingRockPrefab, SpawnPosition, Quaternion.identity);

    }

}

This is the code from my "Difficulty.GetDifficultyPercent"

    public static class Difficulty{
static float secondsToMaxDifficulty = 30;

public static float GetDifficultyPercent()
{
    return Mathf.Clamp01(Time.timeSinceLevelLoad / secondsToMaxDifficulty);
}}

I'm planning to make a certain event happen when my game bypass a certain stage of a difficulty as it progress but I just don't know what to put it as.

My guess is this,

if(Time.time > Difficulty(25))

but didn't work.

\$\endgroup\$
  • \$\begingroup\$ There arent any functions named 'Difficulty' in the code sample and there is a class but you cannot 'call' class itself in C#. \$\endgroup\$ – wondra Sep 21 at 18:05
0
\$\begingroup\$

Try this:

if(GetDifficultyPercent() > .25f)

As wondra pointed out, you cannot call a class directly in C#, and this method returns a value 0..1.

If you want to be able to call this method with a time parameter, you will need to update your method to something like this:

public static class Difficulty{
   static float secondsToMaxDifficulty = 30;
   public static float GetDifficultyPercent(float time)
   {
       return Mathf.Clamp01(time / secondsToMaxDifficulty);
   }
}

and call it like this:

if(GetDifficultyPercent(Time.time) > .25f)

Bear in mind that Time.time returns the amount of time that the application has been running. So once you add the functionality to restart the game, or add in a pause function, or a main menu, you will have to rework your difficulty timer.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

It looks like your approach should more or less work. It seems like there are probably two different situations where you will need to consider difficulty in your game.

1.) Increase/Decrease probability with regards to difficulty.

private IEnumerator SpawnFallingRockRoutine()
{
    while (playing)
    {
        float spawnCooldown = Mathf.Lerp(minSpawnRate, maxSpawnRate, Difficulty.GetCurrentDifficulty());
        // spawn falling rock logic
        yield return new WaitForSeconds(spawnCooldown);
    }
}

2.) Trigger events only if at a certain difficulty.

private IEnumerator EvilMonkeySpawnRoutine()
{
    while (playing)
    {
        float cooldown = 1.0f;
        if (Difficulty.GetCurrentDifficulty() >= evilMonkeyDifficulty)
        {
            // spawn evil monkey
            cooldown = evilMonkeyCooldown;
        }

        yield return cooldown;
     }
}

You may also want to change how your Difficulty class works to make it a bit more extendable in your project:

public class Difficulty
{
    private static float maxDifficultyTime = 30.0f;
    private static float gameStartTime = 0.0f;
    private static float difficultyIncreaseRate = 1.0f;
    private static float difficultyBump = 0.0f;

    public static void SetStartTime(float time)
    {
        gameStartTime = time;
    }

    // Use if you want to make the game temporarily harder
    public static void BumpUpDifficulty(float increase)
    {
        difficultyBump += increase;
    }

    // Use if you want to make the game temporarily easier
    public static void BumpDownDifficulty(float decrease)
    {
        difficultyBump -= decrease;
    }

    // Gets current difficulty in range of 0.0 - 1.0
    public static float GetCurrentDifficulty()
    {
        return Mathf.Clamp01(GetCurrentDifficultyUnclamped());
    }

    // Get current difficulty in range of min - max
    public static float GetCurrentDifficultyClamped(float minDifficulty, float maxDifficulty)
    {
        return Mathf.Clamp(GetCurrentDifficultyUnclamped(), minDifficulty, maxDifficulty);
    }

    // Gets the raw current difficulty (careful with this one)
    public static float GetCurrentDifficultyUnclamped()
    {
        return difficultyBump + (Time.time - gameStartTime) * (difficultyIncreaseRate / maxDifficultyStartTime);
    }
}

    
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.