I went about this a little differently. Trying to ‘think like a programmer’ as they say. I thought it would make sense to create a new dev/cheat key script so that it could also be used for things not relating to collisions.
First I made a public bool in the collision handler script so I could change if we ran the on collision code from a different script.
public class CollisionHandler : MonoBehaviour
{
[SerializeField] float loadDelay = 2f;
[SerializeField] AudioClip crashSFX;
[SerializeField] AudioClip levelWinSFX;
[SerializeField] ParticleSystem crashParticles;
[SerializeField] ParticleSystem levelWinParticles;
private Scene currentScene;
AudioSource audioSource;
bool isTransitioning = false;
public bool canCollide;
void Start()
{
currentScene = SceneManager.GetActiveScene();
audioSource = GetComponent<AudioSource>();
canCollide = true;
}
void OnCollisionEnter(Collision collision)
{
if (isTransitioning || !canCollide) { return; }
switch (collision.gameObject.tag)
{
case "Friendly":
Debug.Log("Collided with a afriendly object");
break;
case "Finish":
FinishSequence();
break;
default:
Debug.Log("That was life ending");
CrashSequence();
break;
}
}
type or paste code here
Then created another script and attached it to the rocket than can load and reload the level but also toggle canCollide in the collision handler script.
public class DevKeys : MonoBehaviour
{
[SerializeField] bool devKeys;
//MeshCollider m_MeshCollider;
private Scene currentScene;
void Start()
{
//m_MeshCollider = GetComponent<MeshCollider>();
currentScene = SceneManager.GetActiveScene();
}
void Update()
{
if (devKeys)
{
LoadNextLevel();
ReloadLevel();
ToggleCollisions();
}
}
void ToggleCollisions()
{
if (Input.GetKeyDown(KeyCode.C))
{
if (this.GetComponent<CollisionHandler>().canCollide)
{
this.GetComponent<CollisionHandler>().canCollide = false;
}
else if(!this.GetComponent<CollisionHandler>().canCollide)
{
this.GetComponent<CollisionHandler>().canCollide = true;
}
}
}
void ReloadLevel()
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadScene(currentScene.buildIndex);
}
}
void LoadNextLevel()
{
if (Input.GetKeyDown(KeyCode.L))
{
// if there is a scene in the build index after the current scene load that one
if (currentScene.buildIndex < SceneManager.sceneCountInBuildSettings - 1)
{
SceneManager.LoadScene(currentScene.buildIndex + 1);
}
// if we are on the last scene in the build index load the first scene in the build index
else if (currentScene.buildIndex == SceneManager.sceneCountInBuildSettings - 1)
{
SceneManager.LoadScene(0);
}
}
}
}
As you can see at first I tried just disabling the mesh collider, which works in that we just float through everything but not quite what I wanted so commented it out for maybe later.
I also found out that you can’t just disable a script to stop the code from running which caused a lot of confusion, frustration and a fair bit of googling.
But I’m kind of proud of the outcome, it might not be pretty or performative code but it works as far as I can tell. If anyone can see any glaring problems I would appreciate any constructive.