Collision and Level Loader

Hello! Here is the code I used for this section.

In addition for handling ‘C’ and ‘L’ input, I added ‘R’ for restarting the current level.

Input:

    bool collisionOn = true;

    void RespondToDebugKeys()
    {
        if (Input.GetKeyDown(KeyCode.C))
        {
            if (collisionOn)
                collisionOn = false;
            else
                collisionOn = true;
        }
        if (Input.GetKeyDown(KeyCode.R)) //restarts the level
            LoadLevel();
        if (Input.GetKeyDown(KeyCode.L)) //loads next level
            LoadNextLevel();
    }

Collision:

My collision method now has a condition for collision with untagged objects. While collision is disabled, the rest of the game will still function (particle systems and sounds played on collision with other type objects):

            default:
                if (collisionOn) //if collisions are off, no death
                    BeginDeathSequence(); //player starts at level 1
                break;

Level Loading:

    void LoadLevel()
    {
        switch (state)
        {
            case State.Alive:
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
                break;

            case State.Transcending:
                LoadNextLevel();
                break;

            case State.Dying:
                SceneManager.LoadScene(0);
                break;
        }
    }

LoadNextLevel() will load the next level if (in debug) ‘L’ is pressed or when player reaches the finish object in the last level.

    void LoadNextLevel()
    {
        if (SceneManager.GetActiveScene().buildIndex != numMaxLevels - 1)
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
        else
            SceneManager.LoadScene(0);
    }

Privacy & Terms