Guest User

F2U script from baldi's computer

a guest
Nov 17th, 2024
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | Gaming | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI; // Required for the Image component
  3. using System.Collections;
  4.  
  5. public class VoiceLineController : MonoBehaviour
  6. {
  7.     public GameControllerScript GameControllerScript;
  8.     public AudioClip[] voiceLines;  // Array to hold different voice lines
  9.  
  10.     public Image displayImage; // Reference to the UI Image component
  11.     public Sprite voiceLineSprite; // sprites corresponding to each voice line
  12.  
  13.     private AudioSource audioSource; // Reference to the AudioSource component
  14.  
  15.     public float[] cooldowns; // Array to hold cooldown times for each voice line
  16.     private float[] lastPlayedTime; // Track when each voice line was last played
  17.  
  18.     private bool[] conditionStates; // Track the previous state of each condition
  19.  
  20.     void Start()
  21.     {
  22.         if (voiceLines == null || voiceLines.Length == 0)
  23.         {
  24.             Debug.LogWarning("No voice lines assigned!");
  25.             return;
  26.         }
  27.  
  28.         audioSource = GetComponent<AudioSource>();
  29.  
  30.         if (audioSource == null)
  31.         {
  32.             Debug.LogError("No AudioSource found on the GameObject!");
  33.             return;
  34.         }
  35.  
  36.         // Initialize arrays based on the number of voice lines
  37.         lastPlayedTime = new float[voiceLines.Length];
  38.         conditionStates = new bool[voiceLines.Length];
  39.  
  40.         // Initialize cooldowns if not manually set
  41.         if (cooldowns == null || cooldowns.Length != voiceLines.Length)
  42.         {
  43.             cooldowns = new float[voiceLines.Length];
  44.             for (int i = 0; i < cooldowns.Length; i++)
  45.             {
  46.                 cooldowns[i] = 5.0f; // Default cooldown
  47.             }
  48.         }
  49.     }
  50.  
  51.     void Update()
  52.     {
  53.         if (voiceLines == null || voiceLines.Length == 0)
  54.             return;
  55.  
  56.         // Example conditions for playing different voice lines
  57.         CheckAndPlayVoiceLine(0, GameControllerScript.notebooks > 1);
  58.         CheckAndPlayVoiceLine(1, GameControllerScript.item[0] == 11);
  59.         CheckAndPlayVoiceLine(2, GameControllerScript.notebooks > 5);
  60.  
  61.         // Add more conditions as needed for other voice lines
  62.     }
  63.  
  64.     void CheckAndPlayVoiceLine(int index, bool condition)
  65.     {
  66.         if (index < 0 || index >= voiceLines.Length)
  67.         {
  68.             Debug.LogWarning("Voice line index out of range!");
  69.             return;
  70.         }
  71.  
  72.         // If the condition was previously false but now is true, and the cooldown has passed
  73.         if (!conditionStates[index] && condition && (Time.time - lastPlayedTime[index] >= cooldowns[index]))
  74.         {
  75.             PlayVoiceLine(index);
  76.             conditionStates[index] = true; // Update the condition state to true
  77.         }
  78.         else if (!condition) // Reset the condition state if the condition is no longer true
  79.         {
  80.             conditionStates[index] = false;
  81.         }
  82.     }
  83.  
  84.     void PlayVoiceLine(int index)
  85.     {
  86.         if (voiceLines[index] != null)
  87.         {
  88.             audioSource.PlayOneShot(voiceLines[index]);
  89.             lastPlayedTime[index] = Time.time; // Update last played time
  90.             StartCoroutine(ChangeSpriteWithDelay(1.0f)); // Call coroutine to change the sprite after a 1-second delay
  91.         }
  92.         else
  93.         {
  94.             Debug.LogWarning("Voice line at index " + index + " is null!");
  95.         }
  96.     }
  97.     IEnumerator ChangeSpriteWithDelay(float delay)
  98.     {
  99.         yield return new WaitForSeconds(delay);
  100.         displayImage.sprite = voiceLineSprite;
  101.     }
  102. }
  103.  
Tags: Unity baldi
Advertisement
Add Comment
Please, Sign In to add comment