Advertisement
evelynshilosky

OpenCloseDoor

Apr 26th, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class OpenCloseDoor : MonoBehaviour
  6. {
  7.     Animator anim;
  8.     bool playerInTrigger = false;
  9.     public bool doorIsClosed = true;
  10.     public Transform playerTrans;
  11.  
  12.     private void Start()
  13.     {
  14.         anim = GetComponent<Animator>();
  15.         playerTrans = GameObject.FindWithTag("Player").transform;
  16.     }
  17.  
  18.     private void OnTriggerEnter(Collider other)
  19.     {
  20.         if (other.CompareTag("Player"))
  21.         {
  22.             playerInTrigger = true;
  23.         }
  24.     }
  25.  
  26.     private void OnTriggerExit(Collider other)
  27.     {
  28.         if (other.CompareTag("Player"))
  29.         {
  30.             playerInTrigger = false;
  31.         }
  32.     }
  33.  
  34.     private void Update()
  35.     {
  36.         if (playerInTrigger && Input.GetMouseButtonDown(0))
  37.         {
  38.             if (doorIsClosed)
  39.             {
  40.                 anim.SetTrigger("open");
  41.                 doorIsClosed = false;
  42.             }
  43.             else
  44.             {
  45.                 anim.SetTrigger("close");
  46.                 doorIsClosed = true;
  47.             }
  48.         }
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement