Advertisement
Thelegokid4455

Door Script (FPS Tutorial 9)

Jun 23rd, 2020
1,317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class door : MonoBehaviour
  6. {
  7.  
  8.     public GameObject doorObj;
  9.  
  10.     private bool isOpen;
  11.  
  12.     private Vector3 curPos;
  13.     public Vector3 openPos;
  14.     public Vector3 closePos;
  15.  
  16.     public float openSpeed;
  17.  
  18.     public AudioClip useAudio;
  19.  
  20.     public bool canOpen;
  21.  
  22.     // Start is called before the first frame update
  23.     void Start()
  24.     {
  25.        
  26.     }
  27.  
  28.     // Update is called once per frame
  29.     void Update()
  30.     {
  31.        
  32.     }
  33.  
  34.     private void FixedUpdate()
  35.     {
  36.         doorObj.transform.localPosition = Vector3.MoveTowards(doorObj.transform.localPosition, curPos, openSpeed);
  37.  
  38.         if (isOpen)
  39.         {
  40.             curPos = openPos;
  41.         }
  42.         else
  43.         {
  44.             curPos = closePos;
  45.         }
  46.     }
  47.  
  48.     private void OnTriggerStay(Collider other)
  49.     {
  50.         if(other.gameObject.tag == "Player")
  51.         {
  52.             if(Input.GetButtonDown("Use") && canOpen)
  53.             {
  54.                 GetComponent<AudioSource>().PlayOneShot(useAudio);
  55.                 if (isOpen)
  56.                     isOpen = false;
  57.                 else
  58.                     isOpen = true;
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement