Advertisement
evelynshilosky

OpenCloseDoor

Apr 18th, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 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.     public bool doorIsClosed = true;
  9.     public int minDistance = 10;
  10.     public Transform playerTrans;
  11.  
  12.  
  13.     private void Start()
  14.     {
  15.         anim = GetComponent<Animator>();
  16.         playerTrans = GameObject.FindWithTag("Player").transform;
  17.     }
  18.  
  19.     private void Update()
  20.     {
  21.         if (Input.GetMouseButtonDown(0))
  22.         {
  23.             if (Vector3.Distance(this.gameObject.transform.position, playerTrans.position) <= minDistance)
  24.             {
  25.                 if (doorIsClosed)
  26.                 {
  27.                     anim.SetTrigger("open");
  28.                     doorIsClosed = false;
  29.                 }
  30.                 else
  31.                 {
  32.                     anim.SetTrigger("close");
  33.                     doorIsClosed = true;
  34.                 }
  35.             }
  36.         }
  37.     }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement