Advertisement
johnnygoodguy2000

LevelLoader.cs

May 12th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | Gaming | 0 0
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3.  
  4. public class LevelLoader : MonoBehaviour
  5. {
  6.     private bool playerInZone;
  7.  
  8.  
  9.     public string levelToLoad;
  10.  
  11.    
  12.  
  13.     void Start()
  14.     {
  15.         playerInZone = false;
  16.     }
  17.  
  18.     void Update()
  19.     {
  20.         if (Input.GetAxisRaw("Vertical") > 0 && playerInZone)//&& Input.GetKey(KeyCode.W))
  21.         {
  22.             // Load the level when 'W' key is pressed and player is in the zone
  23.             //Application.LoadLevel(levelToLoad);// Load the level gives warning dude to update
  24.             SceneManager.LoadScene(levelToLoad);// Load the level gives error for SceneManager
  25.  
  26.  
  27.  
  28.         }
  29.     }
  30.  
  31.     void OnTriggerEnter2D(Collider2D other)
  32.     {
  33.         if (other.name == "Player")
  34.         {
  35.             // Set playerInZone to true when the player enters the trigger zone
  36.             playerInZone = true;
  37.         }
  38.     }
  39.  
  40.     void OnTriggerExit2D(Collider2D other)
  41.     {
  42.         if (other.name == "Player")
  43.         {
  44.             // Set playerInZone to false when the player exits the trigger zone
  45.             playerInZone = false;
  46.         }
  47.     }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement