Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI; // Required for the Image component
- using System.Collections;
- public class VoiceLineController : MonoBehaviour
- {
- public GameControllerScript GameControllerScript;
- public AudioClip[] voiceLines; // Array to hold different voice lines
- public Image displayImage; // Reference to the UI Image component
- public Sprite voiceLineSprite; // sprites corresponding to each voice line
- private AudioSource audioSource; // Reference to the AudioSource component
- public float[] cooldowns; // Array to hold cooldown times for each voice line
- private float[] lastPlayedTime; // Track when each voice line was last played
- private bool[] conditionStates; // Track the previous state of each condition
- void Start()
- {
- if (voiceLines == null || voiceLines.Length == 0)
- {
- Debug.LogWarning("No voice lines assigned!");
- return;
- }
- audioSource = GetComponent<AudioSource>();
- if (audioSource == null)
- {
- Debug.LogError("No AudioSource found on the GameObject!");
- return;
- }
- // Initialize arrays based on the number of voice lines
- lastPlayedTime = new float[voiceLines.Length];
- conditionStates = new bool[voiceLines.Length];
- // Initialize cooldowns if not manually set
- if (cooldowns == null || cooldowns.Length != voiceLines.Length)
- {
- cooldowns = new float[voiceLines.Length];
- for (int i = 0; i < cooldowns.Length; i++)
- {
- cooldowns[i] = 5.0f; // Default cooldown
- }
- }
- }
- void Update()
- {
- if (voiceLines == null || voiceLines.Length == 0)
- return;
- // Example conditions for playing different voice lines
- CheckAndPlayVoiceLine(0, GameControllerScript.notebooks > 1);
- CheckAndPlayVoiceLine(1, GameControllerScript.item[0] == 11);
- CheckAndPlayVoiceLine(2, GameControllerScript.notebooks > 5);
- // Add more conditions as needed for other voice lines
- }
- void CheckAndPlayVoiceLine(int index, bool condition)
- {
- if (index < 0 || index >= voiceLines.Length)
- {
- Debug.LogWarning("Voice line index out of range!");
- return;
- }
- // If the condition was previously false but now is true, and the cooldown has passed
- if (!conditionStates[index] && condition && (Time.time - lastPlayedTime[index] >= cooldowns[index]))
- {
- PlayVoiceLine(index);
- conditionStates[index] = true; // Update the condition state to true
- }
- else if (!condition) // Reset the condition state if the condition is no longer true
- {
- conditionStates[index] = false;
- }
- }
- void PlayVoiceLine(int index)
- {
- if (voiceLines[index] != null)
- {
- audioSource.PlayOneShot(voiceLines[index]);
- lastPlayedTime[index] = Time.time; // Update last played time
- StartCoroutine(ChangeSpriteWithDelay(1.0f)); // Call coroutine to change the sprite after a 1-second delay
- }
- else
- {
- Debug.LogWarning("Voice line at index " + index + " is null!");
- }
- }
- IEnumerator ChangeSpriteWithDelay(float delay)
- {
- yield return new WaitForSeconds(delay);
- displayImage.sprite = voiceLineSprite;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment