-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadLevelsTutorial.cs
More file actions
112 lines (96 loc) · 3.8 KB
/
LoadLevelsTutorial.cs
File metadata and controls
112 lines (96 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Updated LoadLevelAfterWait.cs
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class LoadLevelAfterWait : MonoBehaviour
{
[Tooltip("The name of the scene to load after the wait period.")]
public string sceneToLoad;
[Tooltip("The wait time in seconds before the scene is loaded.")]
public float waitTime = 210f; // Default: 3.5 minutes
void Start()
{
// Start the coroutine that handles scene loading after a delay.
StartCoroutine(LoadSceneAfterDelay());
}
IEnumerator LoadSceneAfterDelay()
{
// Log the start of the coroutine for debugging purposes.
Debug.Log($"Started waiting at timestamp: {Time.time}");
// Wait for the specified duration.
yield return new WaitForSeconds(waitTime);
// Log the scene loading action and load the specified scene.
Debug.Log($"Loading scene: {sceneToLoad} at timestamp: {Time.time}");
SceneManager.LoadScene(sceneToLoad);
}
}
/*
**How to Use LoadLevelAfterWait.cs**
1. Attach this script to any GameObject in your scene.
2. Set the `sceneToLoad` property to the name of the scene you want to load after the wait.
- Make sure the scene is added to the Build Settings.
3. Set the `waitTime` property to the desired wait duration in seconds.
4. Run the game, and the script will load the specified scene after the defined wait time.
*/
// Updated OnTriggerLoadLevel.cs
using UnityEngine;
using UnityEngine.SceneManagement;
public class OnTriggerLoadLevel : MonoBehaviour
{
[Tooltip("The UI text that prompts the player to interact.")]
public GameObject enterText;
[Tooltip("The name of the scene to load when triggered.")]
public string sceneToLoad;
void Start()
{
// Ensure the interaction text is hidden when the game starts.
if (enterText != null)
{
enterText.SetActive(false);
}
}
void OnTriggerStay(Collider other)
{
// Check if the object colliding with the trigger is the player.
if (other.CompareTag("Player"))
{
if (enterText != null)
{
enterText.SetActive(true);
}
// Check for player input to load the scene.
if (Input.GetButtonDown("Use"))
{
Debug.Log($"Loading scene: {sceneToLoad}");
SceneManager.LoadScene(sceneToLoad);
}
}
}
void OnTriggerExit(Collider other)
{
// Hide the interaction text when the player leaves the trigger area.
if (other.CompareTag("Player") && enterText != null)
{
enterText.SetActive(false);
}
}
}
/*
**How to Use LoadLevelAfterWait.cs**
1. Attach this script to any GameObject in your scene.
2. Set the `sceneToLoad` property to the name of the scene you want to load after the wait.
- Make sure the scene is added to the Build Settings.
3. Set the `waitTime` property to the desired wait duration in seconds.
4. Run the game, and the script will load the specified scene after the defined wait time.
**How to Use OnTriggerLoadLevel.cs**
1. Attach this script to a GameObject with a Collider set to "Is Trigger".
2. Assign the `enterText` property to a UI Text GameObject that displays interaction prompts.
- Ensure this text is hidden by default.
3. Set the `sceneToLoad` property to the name of the scene you want to load.
- Make sure the scene is added to the Build Settings.
4. Tag the player GameObject with "Player" and ensure it has a Collider.
5. Add an input action named "Use" in Unity's Input Manager (Edit > Project Settings > Input Manager).
- Default: Map it to a key, e.g., "E".
6. When the player enters the trigger area, the interaction prompt will appear.
- Press the "Use" key to load the specified scene.
*/