Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class DrawerController : MonoBehaviour
  4. {
  5.     public GameObject drawer;
  6.  
  7.     public float distance;
  8.     public float lerpTime;
  9.  
  10.     private bool isOpen;
  11.     private bool canInteract;
  12.  
  13.     private Vector3 startPos;
  14.     private Vector3 endPos;
  15.  
  16.     private float currentLerpTime = 0;
  17.  
  18.     void Start()
  19.     {
  20.         //audio = GetComponent<AudioSource>();
  21.         // Start position will be updated
  22.         startPos = drawer.transform.position;
  23.  
  24.         // End position will be updated
  25.         endPos = drawer.transform.position + Vector3.right * distance;
  26.     }
  27.  
  28.  
  29.     void Update()
  30.     {
  31.         //If we can interact and we press the E key, flip the isOpen bool
  32.         if (canInteract)
  33.         {
  34.             if (Input.GetKeyDown(KeyCode.E))
  35.             {
  36.                 InteractWithDrawer();
  37.             }
  38.         }
  39.  
  40.         //The drawer is in its proper position:
  41.         //if it's open and it's at the end position OR if it's closed and at the start position
  42.         bool isInPosition = (isOpen && window.transform.position == endPos) || (!isOpen && window.transform.position == startPos);
  43.  
  44.         //If the drawer is not in position, move it!
  45.         if (!isInPosition)
  46.         {
  47.             //If it's meant to be open increase the currentLerpTime
  48.             //Else decrease the currentLerpTime
  49.             if (isOpen)
  50.             {
  51.                 currentLerpTime += Time.deltaTime;
  52.             }
  53.             else currentLerpTime -= Time.deltaTime;
  54.  
  55.             //Clamp the lerp time between 0 and 1, this means it cannot go below 0 or above 1.
  56.             currentLerpTime = Mathf.Clamp01(currentLerpTime);
  57.  
  58.             float move = currentLerpTime / lerpTime;
  59.             window.transform.position = Vector3.Lerp(startPos, endPos, move);
  60.         }
  61.     }
  62.  
  63.     void OnTriggerEnter(Collider other)
  64.     {
  65.         if (other.gameObject.tag == "Player")
  66.         {
  67.             canInteract = true;
  68.         }
  69.     }
  70.  
  71.     void OnTriggerExit(Collider other)
  72.     {
  73.         if (other.gameObject.tag == "Player")
  74.         {
  75.             canInteract = false;
  76.         }
  77.  
  78.     }
  79.  
  80.     //If isOpen is true set it to false, if isOpen is false set it to true
  81.     public void InteractWithDrawer()
  82.     {
  83.         isOpen = !isOpen;
  84.         Debug.Log(isOpen);
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement