Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class DoorController : MonoBehaviour
  6. {
  7. [SerializeField] ItemType key; //The type of item to be used as the doors 'key'
  8. [SerializeField] bool isLocked = false; //Whether or not the door is locked. If false, we dont search our inventory for the key
  9. [SerializeField] Vector3 closedRotation; //Rotation that the door should sit at when closed
  10. [SerializeField] Vector3 openRotation; //Rotation that the door should sit at when open
  11. [SerializeField] float rotateSpeed = 1.0f; //The speed that the door will swing between rotations
  12.  
  13. [SerializeField] GameObject doorHinge; //A reference to the empty GameObject that stores our door. We rotate this so that we can control the pivot par.
  14.  
  15. bool isOpen = false; //Whether or not the door is open
  16.  
  17. //Upate is called once per frame
  18. private void Update()
  19. {
  20. //If the door should be open...
  21. if (isOpen)
  22. {
  23. //Test to see if our hinges rotaion is equal to our desired open rotation
  24. if (doorHinge.transform.rotation != Quaternion.Euler(openRotation))
  25. {
  26. //If not, then use the Quaternion.RotateTowards method to rotate the door.
  27. doorHinge.transform.rotation = Quaternion.RotateTowards(doorHinge.transform.rotation, Quaternion.Euler(openRotation), rotateSpeed);
  28. }
  29. }
  30. //If the door should not open, then rotate towards the closed rotation
  31. else
  32. {
  33. if (doorHinge.transform.rotation != Quaternion.Euler(closedRotation))
  34. {
  35. doorHinge.transform.rotation = Quaternion.RotateTowards(doorHinge.transform.rotation, Quaternion.Euler(closedRotation), rotateSpeed);
  36. }
  37. }
  38. }
  39.  
  40. //This method is called automatically every frame when there is another collider inside our trigger
  41. private void OnTriggerStay(Collider other)
  42. {
  43. //If the other collider is tagged with "Player"
  44. if (other.tag == "Player")
  45.  
  46. {
  47. //If the player presses soace
  48. if (Input.GetKeyDown(KeyCode.Space))
  49. {
  50. //If either the door is unlocked OR if the player has the key in their inventory
  51. if ((isLocked == false) || (Inventory.Instance.SearchItem(key)))
  52. {
  53. //Set 'isOpen' to the opposite of what it was before
  54. isOpen = !isOpen;
  55. }
  56. }
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement