Winning zone

Similar to what we did for the Water zone, we need to create an endpoint for our map. Once the player reaches the endpoint, the game will show a message (left as an exercise) and load the next level. Since the structure is so similar to the Water zone, here is the direct script, which should be self-explanatory:

using UnityEngine;
using UnityEngine.SceneManagement;

[RequireComponent(typeof(Collider2D))]
public class WinningZone : MonoBehaviour {

[SerializeField]
private float timeDelay = 1.0f;

[SerializeField]
private string nextLevelToLoad = "level2";
//substitute with the next level to load.
Be sure it is included in the build settings.

private IEnumerator loadNextLevel() {
//Show a winning message (left as exercise)

//Wait timeDelay
yield return new WaitForSeconds(timeDelay);

//Restart Scene
SceneManager.LoadScene(nextLevelToLoad);
//load the level named as in the nextLevelToLoad variable.
}

private void OnTriggerEnter2D(Collider2D collision) {
//If the player enters within the Water Zone,
then restart the scene
if (collision.CompareTag("Player")) {
StartCoroutine(loadNextLevel());
}
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset