Debug Mode

So i managed to write some code all on my own… and actually got it to work. It is a little temprimental but it works.

So the way my code works if the player presses the ‘C’ key while not already in debug mode it changes the state to ‘Debug’.

While in Debug Mode the player can press L to instantly win the level, or can fly through the level without risk of dying as the collisions only work if the gamestate is set to ‘Alive’

If the player presses the ‘C’ key while in debug mode it switches the state into ‘Alive’ making the game run like normal.

Please see my code below, any tips on cleaning it up or on a clearer structure would be greatly appreciated

using System;
using UnityEngine;
using UnityEngine.SceneManagement;

public class rocket : MonoBehaviour
{
    [SerializeField] float rcsThrust = 100f;
    [SerializeField] float mainThrust = 100f;
    [SerializeField] float levelLoadDelay = 1f;

    [SerializeField] AudioClip mainEngine;
    [SerializeField] AudioClip success;
    [SerializeField] AudioClip death;

    [SerializeField] ParticleSystem mainEngineParticles;
    [SerializeField] ParticleSystem successParticles;
    [SerializeField] ParticleSystem deathParticles;

    Rigidbody rigidBody;
    AudioSource audioSource;

    enum State { Alive, Dying, Transcending, Debug }
    State state = State.Alive;

    // Use this for initialization
    void Start()
    {
        rigidBody = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        if (state == State.Alive)
        {
            RespondToThrustInput();
            RespondToRotateInput();
            ToggleDebugMode();
        }

        else if (state == State.Debug)
        {
            RespondToThrustInput();
            RespondToRotateInput();
            ToggleDebugMode();
            SkipLevel();
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        if (state != State.Alive) { return; } // ignore collisions when dead // could  use this to toggle collisions?

        switch (collision.gameObject.tag)
        {
            case "Friendly":
                // do nothing
                break;
            case "Finish":
                StartSuccessSequence();
                break;
            default:
                StartDeathSequence();
                break;
        }
    }

    private void StartSuccessSequence()
    {
        state = State.Transcending;
        audioSource.Stop();
        audioSource.PlayOneShot(success);
        successParticles.Play();
        Invoke("LoadNextLevel", levelLoadDelay);
    }

    private void StartDeathSequence()
    {
        state = State.Dying;
        audioSource.Stop();
        audioSource.PlayOneShot(death);
        deathParticles.Play();
        Invoke("LoadFirstLevel", levelLoadDelay);
    }

    private void LoadNextLevel()
    {
        SceneManager.LoadScene(1); // todo allow for more than 2 levels
    }

    private void LoadFirstLevel()
    {
        SceneManager.LoadScene(0);
    }

    private void RespondToThrustInput()
    {
        if (Input.GetKey(KeyCode.Space)) // can thrust while rotating
        {
            ApplyThrust();
        }
        else
        {
            audioSource.Stop();
            mainEngineParticles.Stop();
        }
    }

    private void ApplyThrust()
    {
        rigidBody.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
        if (!audioSource.isPlaying) // so it doesn't layer
        {
            audioSource.PlayOneShot(mainEngine);
        }
        mainEngineParticles.Play();
    }

    private void RespondToRotateInput()
    {
        rigidBody.freezeRotation = true; // take manual control of rotation

        float rotationThisFrame = rcsThrust * Time.deltaTime;
        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward * rotationThisFrame);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(-Vector3.forward * rotationThisFrame);
        }

        rigidBody.freezeRotation = false; // resume physics control of rotation
    }
    void ToggleDebugMode()
    {
        if (Input.GetKey(KeyCode.C))
        {
            if (state != State.Debug)
            {
                state = State.Debug;
            }
            else
            {
                state = State.Alive;
            }
        }
                    
    }

    void SkipLevel()
    {
        if (Input.GetKey(KeyCode.L))
        {
            StartSuccessSequence();
        }
    }
}
1 Like

I’m not sure about the code but I’m proud of you for implementing your own thing! Keep on going of the beaten path.

Hey mate, So i have decided to go with the course method, because that is what we are being taught as good practice. I would, however, like to know what makes you unsure of the code i wrote, what issues could you foresee if i’d actually tried to implement it?

Oh my bad. I am not advanced enough to give any comment on the code. What I meant to say was regardless of how it is It’s awesome you did your own thing. In my opinion it looks good :grin:

Privacy & Terms