Advertisement
vjanomolee

KeyDetector.cs

Jan 31st, 2023
1,206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using System;
  6.  
  7. public class KeyDetector : MonoBehaviour
  8. {
  9.     private TextMeshProUGUI display;
  10.     public int correctCombination;
  11.     public bool accessGranted = false;
  12.     public GameObject keyCard;
  13.     public GameObject iKeycard;
  14.     public Transform spawnPoint;
  15.     private AudioSource audioSource;
  16.     public AudioClip loseSound;
  17.     public AudioClip winSound;
  18.  
  19.     void Start()
  20.     {
  21.         display = GameObject.FindGameObjectWithTag("Display").GetComponentInChildren<TextMeshProUGUI>();
  22.         display.text = "Enter the right number combo to get keycard";
  23.         audioSource = gameObject.AddComponent<AudioSource>();
  24.         audioSource.spatialBlend =1;
  25.         audioSource.volume = 0.2f;
  26.     }
  27.     private void OnTriggerEnter(Collider other)
  28.     {
  29.         if(other.CompareTag("KeypadButton"))
  30.         {
  31.             //Debug.Log("Your touching it");
  32.             var key = other.GetComponentInChildren<TextMeshProUGUI>();
  33.             if(key != null)
  34.             {
  35.                 Debug.Log("Your key is NOT null bruh you made it a level deeper");
  36.                 var KeyFeedback = other.gameObject.GetComponent<KeyFeedback>();
  37.                 if(key.text == "<")
  38.                 {
  39.                     Debug.Log("Your key is back arrow");
  40.                     if(display.text.Length > 0)
  41.                     {
  42.                         display.text = display.text.Substring(0, display.text.Length - 1);
  43.                     }
  44.                 }
  45.                 else if(key.text == "go")
  46.                 {
  47.                     bool accessGranted = false;
  48.                     bool onlyNumbers = int.TryParse(display.text, out int value); // not sure if I need this here or not
  49.                     if(onlyNumbers == true && display.text.Length > 0) //might need to add this back to the if statement: onlyNumbers == true &&
  50.                     {
  51.                        accessGranted = CheckIfCorrect(Convert.ToInt32(display.text));
  52.                     }
  53.                     if(accessGranted == true)
  54.                     {
  55.                         GameObject iKeycard = Instantiate(keyCard, spawnPoint.transform.position, spawnPoint.transform.rotation);
  56.                         display.text = "You did it! Congratulations. Now pick up the keycard and use it to open the door";
  57.                         audioSource.PlayOneShot(winSound);
  58.  
  59.                         accessGranted = false;
  60.  
  61.                     }
  62.                     else
  63.                     {
  64.                         display.text = "nah bruh you got it wrong, hit the X button clear it and try again";
  65.                         audioSource.PlayOneShot(loseSound);
  66.                     }
  67.                 }
  68.                 else if(key.text == "X")
  69.                 {
  70.                     display.text = "";
  71.                 }
  72.                 else
  73.                 {
  74.                      //test if there is letters on the display, if so empty display before adding new number
  75.                         bool onlyNumbers = int.TryParse(display.text, out int value);
  76.                         if(onlyNumbers == false)
  77.                         {
  78.                             display.text = "";
  79.                         }
  80.  
  81.                         //make sure that there is max 4 numbers on the display
  82.                         if(display.text.Length < 4)
  83.                         {
  84.                             display.text += key.text;
  85.                         }
  86.                 }
  87.                 KeyFeedback.keyHit = true;
  88.             }
  89.         }
  90.     }
  91.     public bool CheckIfCorrect(int combination)
  92.     {
  93.         if(combination == correctCombination)
  94.         {
  95.             accessGranted = true;
  96.             return true;
  97.         }
  98.         return false;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement