Advertisement
gandalfbialy

Untitled

Apr 12th, 2025
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Net.Sockets;
  4. using UnityEngine;
  5.  
  6. public class Lock : MonoBehaviour
  7. {
  8.     bool canOpen = false;
  9.     public Door[] doors;
  10.     public KeyColor keyColor;
  11.     public bool isLocked = false;
  12.     Animator key;
  13.  
  14.     // Start is called before the first frame update
  15.     void Start()
  16.     {
  17.         key = GetComponent<Animator>();
  18.     }
  19.  
  20.     // Update is called once per frame
  21.     void Update()
  22.     {
  23.         if (canOpen && !isLocked)
  24.         {
  25.             GameManager.gameManager.SetUseInfo("Press E to open lock");
  26.         }
  27.  
  28.         if (Input.GetKeyDown(KeyCode.E) && canOpen && !isLocked)
  29.         {
  30.             key.SetBool("useKey", CheckTheKey());
  31.         }
  32.     }
  33.  
  34.     public void UseKey()
  35.     {
  36.         foreach (Door door in doors)
  37.         {
  38.             door.Open();
  39.         }
  40.     }
  41.  
  42.     public bool CheckTheKey()
  43.     {
  44.         if (GameManager.gameManager.redKey > 0 && keyColor == KeyColor.Red)
  45.         {
  46.             GameManager.gameManager.redKey--;
  47.             GameManager.gameManager.redKeyText.text = GameManager.gameManager.redKey.ToString();
  48.  
  49.             isLocked = true;
  50.             return true;
  51.         }
  52.         else if (GameManager.gameManager.greenKey > 0 && keyColor == KeyColor.Green)
  53.         {
  54.             GameManager.gameManager.greenKey--;
  55.             GameManager.gameManager.greenKeyText.text = GameManager.gameManager.greenKey.ToString();
  56.  
  57.             isLocked = true;
  58.             return true;
  59.         }
  60.         else if (GameManager.gameManager.goldKey > 0 && keyColor == KeyColor.Gold)
  61.         {
  62.             GameManager.gameManager.goldKey--;
  63.             GameManager.gameManager.goldKeyText.text = GameManager.gameManager.goldKey.ToString();
  64.  
  65.             isLocked = true;
  66.             return true;
  67.         }
  68.         else
  69.         {
  70.             Debug.Log("Nie masz klucza!");
  71.             return false;
  72.         }
  73.     }
  74.  
  75.     private void OnTriggerEnter(Collider other)
  76.     {
  77.         if (other.CompareTag("Player"))
  78.         {
  79.             canOpen = true;
  80.             Debug.Log("You can open the door now!");
  81.         }
  82.     }
  83.  
  84.     private void OnTriggerExit(Collider other)
  85.     {
  86.         if (other.CompareTag("Player"))
  87.         {
  88.             canOpen = false;
  89.             GameManager.gameManager.SetUseInfo("");
  90.  
  91.             Debug.Log("You can not open the door :(");
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement