My Cheat/Debug solution

I took a different path to enabling cheats in my game than Rick did. First I created a Cheat script and attached that to my rocket. Within there I have the following code:

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

public class Cheat : MonoBehaviour {
	bool IsCheatEnabled = true;

	CollisionHandler CollisionHandler;

	// Start is called before the first frame update
	void Start() {
		CollisionHandler = GetComponent<CollisionHandler>();
	}

	// Update is called once per frame
	void Update() {
		if (IsCheatEnabled == false) { return; }
		if (Input.GetKeyDown(KeyCode.C)) {
			CollisionHandler.ToggleCollision();
		} else if (Input.GetKeyDown(KeyCode.P)) {
			CollisionHandler.LoadPreviousScene();
		} else if (Input.GetKeyDown(KeyCode.R)) {
			CollisionHandler.ReloadScene();
		} else if (Input.GetKeyDown(KeyCode.N)) {
			CollisionHandler.LoadNextScene();
		}
	}
}

I still handled all of the actual logic within CollisionHandler (hence the CollisionHandler cache member at the top), but it didn’t feel right to have the Input code in there as well. This will also make it easy to disable the entire thing later if I want by simply changing IsCheatEnabled to false at the top of the script.

I also added the ability to go back to the previous level or to reload the current level (one time I got so lost flying off in a random direction that I couldn’t find my way back, so this could be useful in that sort of situation).

Then within CollisionHandler I obviously needed the four methods I’m calling, which look like this:

	bool IsCollisionDisabled = false;

	public void LoadNextScene() {
		StartCoroutine(LoadNextSceneAfterDelay(0));
	}

	public void LoadPreviousScene() {
		StartCoroutine(LoadPreviousSceneAfterDelay(0));
	}

	public void ReloadScene() {
		StartCoroutine(ReloadSceneAfterDelay(0));
	}

	public void ToggleCollision() {
		IsCollisionDisabled = !IsCollisionDisabled;
	}

And then within OnCollisionEnter:

if (IsCollisionDisabled) { return; }

The load next and reload methods just use the methods I was already using when landing or crashing, and the logic for LoadPreviousSceneAfterDelay was basically the same logic as LoadNextSceneAfterDelay except I was subtracting 1 and then checking if the value was < 0 rather than adding 1.

Everything works exactly as expected and I have the framework available for adding additional cheats in the future (already thinking of adding F to refill your fuel (or maybe just disabling fuel from dropping as you use it) if I end up adding a fuel mechanic into my game).

Privacy & Terms