Hi there I am currently working on the rocket boost program by Rick Davidson.
And after implementing the Debug/Cheat Codes when I start my Game it won`t recognize any Collision at all.
Even without pressing the C Key.
Here is my Code:
using UnityEngine;
using UnityEngine.SceneManagement;
public class CollisionHandler : MonoBehaviour
{
AudioSource audioSource;
[SerializeField] float invokeTime = 1f;
[SerializeField] AudioClip obstacleCollission;
[SerializeField] AudioClip reachedFinish;
[SerializeField] ParticleSystem collissionParticles;
[SerializeField] ParticleSystem finishParticles;
bool isTransitioning = false;
bool collisionDisabled = false;
// Start is called before the first frame update
void Start()
{
audioSource = GetComponent<AudioSource>();
}
void Update()
{
RespondToDebugKeys();
}
void RespondToDebugKeys()
{
if (Input.GetKeyDown(KeyCode.L))
{
LoadNextLevel();
}
else if (Input.GetKeyDown(KeyCode.C))
{
collisionDisabled = !collisionDisabled; // toggle Collision
}
void OnCollisionEnter(Collision other)
{
if (isTransitioning || collisionDisabled) { return; }
switch (other.gameObject.tag)
{
case "Friendly":
Debug.Log("This Object is friendly");
break;
// Fuel will not be used
case "Fuel":
Debug.Log("This is Fuel");
break;
case "Finish":
StartFinishSequence();
break;
default:
StartCrashSequence();
break;
}
}
void StartFinishSequence()
{
isTransitioning = true;
audioSource.Stop();
audioSource.PlayOneShot(reachedFinish);
finishParticles.Play();
GetComponent<Movement>().enabled = false;
Invoke("LoadNextLevel", invokeTime);
}
void StartCrashSequence()
{
isTransitioning = true;
audioSource.Stop();
audioSource.PlayOneShot(obstacleCollission);
collissionParticles.Play();
GetComponent<Movement>().enabled = false;
Invoke("ReloadLevel", invokeTime);
}
void LoadNextLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int nextSceneIndex = currentSceneIndex + 1;
if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
{
nextSceneIndex = 0;
}
SceneManager.LoadScene(nextSceneIndex);
}
void ReloadLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex);
}
}
}