Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- using System;
- public class KeyDetector : MonoBehaviour
- {
- private TextMeshProUGUI display;
- public int correctCombination;
- public bool accessGranted = false;
- public GameObject keyCard;
- public GameObject iKeycard;
- public Transform spawnPoint;
- private AudioSource audioSource;
- public AudioClip loseSound;
- public AudioClip winSound;
- void Start()
- {
- display = GameObject.FindGameObjectWithTag("Display").GetComponentInChildren<TextMeshProUGUI>();
- display.text = "Enter the right number combo to get keycard";
- audioSource = gameObject.AddComponent<AudioSource>();
- audioSource.spatialBlend =1;
- audioSource.volume = 0.2f;
- }
- private void OnTriggerEnter(Collider other)
- {
- if(other.CompareTag("KeypadButton"))
- {
- //Debug.Log("Your touching it");
- var key = other.GetComponentInChildren<TextMeshProUGUI>();
- if(key != null)
- {
- Debug.Log("Your key is NOT null bruh you made it a level deeper");
- var KeyFeedback = other.gameObject.GetComponent<KeyFeedback>();
- if(key.text == "<")
- {
- Debug.Log("Your key is back arrow");
- if(display.text.Length > 0)
- {
- display.text = display.text.Substring(0, display.text.Length - 1);
- }
- }
- else if(key.text == "go")
- {
- bool accessGranted = false;
- bool onlyNumbers = int.TryParse(display.text, out int value); // not sure if I need this here or not
- if(onlyNumbers == true && display.text.Length > 0) //might need to add this back to the if statement: onlyNumbers == true &&
- {
- accessGranted = CheckIfCorrect(Convert.ToInt32(display.text));
- }
- if(accessGranted == true)
- {
- GameObject iKeycard = Instantiate(keyCard, spawnPoint.transform.position, spawnPoint.transform.rotation);
- display.text = "You did it! Congratulations. Now pick up the keycard and use it to open the door";
- audioSource.PlayOneShot(winSound);
- accessGranted = false;
- }
- else
- {
- display.text = "nah bruh you got it wrong, hit the X button clear it and try again";
- audioSource.PlayOneShot(loseSound);
- }
- }
- else if(key.text == "X")
- {
- display.text = "";
- }
- else
- {
- //test if there is letters on the display, if so empty display before adding new number
- bool onlyNumbers = int.TryParse(display.text, out int value);
- if(onlyNumbers == false)
- {
- display.text = "";
- }
- //make sure that there is max 4 numbers on the display
- if(display.text.Length < 4)
- {
- display.text += key.text;
- }
- }
- KeyFeedback.keyHit = true;
- }
- }
- }
- public bool CheckIfCorrect(int combination)
- {
- if(combination == correctCombination)
- {
- accessGranted = true;
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement