Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class DoorController : MonoBehaviour
- {
- [SerializeField] ItemType key; //The type of item to be used as the doors 'key'
- [SerializeField] bool isLocked = false; //Whether or not the door is locked. If false, we dont search our inventory for the key
- [SerializeField] Vector3 closedRotation; //Rotation that the door should sit at when closed
- [SerializeField] Vector3 openRotation; //Rotation that the door should sit at when open
- [SerializeField] float rotateSpeed = 1.0f; //The speed that the door will swing between rotations
- [SerializeField] GameObject doorHinge; //A reference to the empty GameObject that stores our door. We rotate this so that we can control the pivot par.
- bool isOpen = false; //Whether or not the door is open
- //Upate is called once per frame
- private void Update()
- {
- //If the door should be open...
- if (isOpen)
- {
- //Test to see if our hinges rotaion is equal to our desired open rotation
- if (doorHinge.transform.rotation != Quaternion.Euler(openRotation))
- {
- //If not, then use the Quaternion.RotateTowards method to rotate the door.
- doorHinge.transform.rotation = Quaternion.RotateTowards(doorHinge.transform.rotation, Quaternion.Euler(openRotation), rotateSpeed);
- }
- }
- //If the door should not open, then rotate towards the closed rotation
- else
- {
- if (doorHinge.transform.rotation != Quaternion.Euler(closedRotation))
- {
- doorHinge.transform.rotation = Quaternion.RotateTowards(doorHinge.transform.rotation, Quaternion.Euler(closedRotation), rotateSpeed);
- }
- }
- }
- //This method is called automatically every frame when there is another collider inside our trigger
- private void OnTriggerStay(Collider other)
- {
- //If the other collider is tagged with "Player"
- if (other.tag == "Player")
- {
- //If the player presses soace
- if (Input.GetKeyDown(KeyCode.Space))
- {
- //If either the door is unlocked OR if the player has the key in their inventory
- if ((isLocked == false) || (Inventory.Instance.SearchItem(key)))
- {
- //Set 'isOpen' to the opposite of what it was before
- isOpen = !isOpen;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement