-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlaysoundAndVideoTutorials.cs
More file actions
110 lines (90 loc) · 3.13 KB
/
PlaysoundAndVideoTutorials.cs
File metadata and controls
110 lines (90 loc) · 3.13 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
// Updated scripts for Unity (latest version)
// Includes implementation tutorials
// PlaySound.cs
using UnityEngine;
/// <summary>
/// This script plays a sound when the player enters a trigger zone.
/// Attach it to the GameObject with the trigger collider.
/// </summary>
[RequireComponent(typeof(AudioSource))]
public class PlaySound : MonoBehaviour {
public AudioClip SoundToPlay;
[Range(0f, 1f)] public float Volume = 1.0f;
private AudioSource _audioSource;
private bool _alreadyPlayed = false;
void Start() {
_audioSource = GetComponent<AudioSource>();
}
void OnTriggerEnter(Collider other) {
if (!_alreadyPlayed && other.CompareTag("Player")) {
_audioSource.PlayOneShot(SoundToPlay, Volume);
_alreadyPlayed = true;
}
}
}
/*
**Tutorial:**
1. Attach this script to a GameObject with a trigger collider.
2. Assign the AudioClip in the inspector.
3. Ensure the "Player" GameObject has the tag "Player."
4. Attach an AudioSource component to the GameObject and configure the settings as needed.
*/
// PlaySound1.cs
using UnityEngine;
/// <summary>
/// This script plays a sound when the player enters a trigger zone (variation 1).
/// Attach it to the GameObject with the trigger collider.
/// </summary>
[RequireComponent(typeof(AudioSource))]
public class PlaySound1 : MonoBehaviour {
public AudioClip SoundToPlay;
[Range(0f, 1f)] public float Volume = 1.0f;
private AudioSource _audioSource;
private bool _alreadyPlayed = false;
void Start() {
_audioSource = GetComponent<AudioSource>();
}
void OnTriggerEnter(Collider other) {
if (!_alreadyPlayed && other.CompareTag("Player")) {
_audioSource.PlayOneShot(SoundToPlay, Volume);
_alreadyPlayed = true;
}
}
}
/*
**Tutorial:**
1. Similar to PlaySound.cs, attach this script to a GameObject with a trigger collider.
2. Assign an AudioClip in the inspector.
3. Make sure the "Player" GameObject has the tag "Player."
4. Add an AudioSource component to the GameObject and configure it as needed.
*/
// PlayVideo.cs
using UnityEngine;
/// <summary>
/// This script activates a video player GameObject when the player enters a trigger zone.
/// Attach it to a GameObject with a trigger collider.
/// </summary>
public class PlayVideo : MonoBehaviour {
public GameObject videoPlayer;
public float timeToStop = 5.0f;
void Start() {
if (videoPlayer != null)
videoPlayer.SetActive(false);
else
Debug.LogError("VideoPlayer GameObject not assigned.");
}
void OnTriggerEnter(Collider other) {
if (other.CompareTag("Player") && videoPlayer != null) {
videoPlayer.SetActive(true);
Destroy(videoPlayer, timeToStop);
}
}
}
/*
**Tutorial:**
1. Attach this script to a GameObject with a trigger collider.
2. Assign a video player GameObject to the `videoPlayer` field in the inspector.
3. Make sure the "Player" GameObject has the tag "Player."
4. Set the `timeToStop` variable to define how long the video will play.
5. Ensure the video player GameObject is initially inactive.
*/