Newer
Older
Dreamsturbia-Project-IADE-Unity3D / Assets / Scripts / PortalBehaviour.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.SceneManagement;
  6. public class PortalBehaviour : MonoBehaviour
  7. {
  8. public Vector3 destination;
  9. [Tooltip("target scene use -1 to disable it")] public int sceneId = -1;
  10. [Tooltip("game objects to enable on teleporting")]public GameObject[] entities;
  11. [Tooltip("the teleport destination becomes relative to the Portal")]public bool relative;
  12. public UnityEvent<PortalBehaviour,Transform> onTeleported;
  13. [Tooltip("enables the Collision Detection (Trigger) automatic teleport")]public bool enableAutomaticTeleport = true;
  14. // Start is called before the first frame update
  15. private void OnTriggerStay(Collider collision)
  16. {
  17. if (enableAutomaticTeleport)
  18. {
  19. if (collision.gameObject.CompareTag("Player"))
  20. {
  21. Teleport(collision.transform);
  22. }
  23. }
  24. }
  25. public void Teleport(Transform target)
  26. {
  27. StoredTeleport.position = destination;
  28. StoredTeleport.teleported = true;
  29. if (relative)
  30. {
  31. target.position = transform.position + destination;
  32. }
  33. else
  34. {
  35. target.position = destination;
  36. }
  37. if (sceneId != -1)
  38. {
  39. SceneManager.LoadScene(sceneId);
  40. }
  41. if (entities != null && entities.Length > 0)
  42. {
  43. foreach (GameObject entity in entities)
  44. {
  45. if (entity != null)
  46. {
  47. if (!entity.activeSelf)
  48. {
  49. entity.SetActive(true);
  50. }
  51. }
  52. }
  53. }
  54. onTeleported?.Invoke(this,target);
  55. }
  56. }