Improving the code thanks all the teach by Rick!

Actually in the last video I did manage to improve my code, so I did share with you all my entire code of CollisionHandler and make more readable as Rick sais:

public class CollisionHandler : MonoBehaviour

{

//Parameter

//cache

//State

[SerializeField] float secondsToWait;

[SerializeField] AudioClip crashSound;

[SerializeField] AudioClip winningSound;

AudioClip currentAudio;

string sceneController;

AudioSource audioSound;

bool isTransitioning = false;

void Start()

{

    audioSound = GetComponent<AudioSource>();

}

void OnCollisionEnter(Collision other)

{

   if(isTransitioning){return;}

   switch(other.gameObject.tag)

   {

    case "Start":

        Debug.Log("Es el punto de partida");

        break;

    case "Finish":            

        sceneController = "LoadNextLevel";

        currentAudio = winningSound;

        LoadingManager(sceneController);

        break;

    case "Ground":

        Debug.Log("has tocado el suelo");

        break;

    case "Obstacle":

        sceneController = "ReloadScene";

        currentAudio = crashSound;

        LoadingManager(sceneController);

        break;

    }

}

void LoadingManager (string scene)

{

    isTransitioning = true;

    audioSound.Stop();

    audioSound.PlayOneShot(currentAudio);

    GetComponent<movement>().enabled = false;

    Invoke(scene, secondsToWait);

}

//////////////Winning Time/////////////////

void LoadNextLevel()

{

    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

    int nextSceneIndex = currentSceneIndex +1;

    if(nextSceneIndex == SceneManager.sceneCountInBuildSettings)

    {

        nextSceneIndex = 0;

    }

    SceneManager.LoadScene(nextSceneIndex);

}

////////////////Losing time////////////////

void ReloadScene()

{

    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

    SceneManager.LoadScene(currentSceneIndex);

}

}

1 Like

Privacy & Terms