Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- using UnityEngine.AI;
- using UnityEngine.SceneManagement;
- using Sirenix.OdinInspector;
- namespace RPG.SceneManagement
- {
- public class Portal : MonoBehaviour
- {
- enum DestinationIdentifier
- {
- A, B, C, D, E, F, G, H, I, J, K, L, M,
- N, O, P, Q, R, S, T, U, V, W, X, Y, Z
- }
- // Variables
- [BoxGroup("Portal Info")]
- [GUIColor(1f, 0.3f, 0.3f)]
- [SerializeField] int sceneToLoad = -1;
- [BoxGroup("Portal Info")]
- [GUIColor(0.3f, 0.3f, 1f)]
- [SerializeField] Transform spawnPoint;
- [BoxGroup("Portal Info")]
- [GUIColor(0.3f, 01f, 0.3f)]
- [SerializeField] DestinationIdentifier destination;
- [BoxGroup("Portal Info")]
- [GUIColor(0.3f, 0.3f, 0.3f)]
- [Range(0f, 10f)]
- [SerializeField] float fadeOutTime = 1f;
- [BoxGroup("Portal Info")]
- [GUIColor(1f, 0.3f, 1f)]
- [Range(0f, 10f)]
- [SerializeField] float fadeInTime = 2f;
- [BoxGroup("Portal Info")]
- [GUIColor(0f, 1f, 0.3f)]
- [Range(0f, 10f)]
- [SerializeField] float fadeWaitTime = 0.5f;
- void OnTriggerEnter(Collider other)
- {
- if(other.tag == "Player")
- {
- StartCoroutine(Transition());
- }
- }
- private IEnumerator Transition()
- {
- if(sceneToLoad < 0)
- {
- Debug.LogError("Scene to load not set.");
- yield break;
- }
- DontDestroyOnLoad(gameObject);
- Fader fader = FindObjectOfType<Fader>();
- yield return fader.FadeOut(fadeOutTime);
- SavingWrapper wrapper = FindObjectOfType<SavingWrapper>();
- wrapper.Save();
- yield return SceneManager.LoadSceneAsync(sceneToLoad);
- wrapper.Load();
- Portal otherPortal = GetOtherPortal();
- UpdatePlayer(otherPortal);
- yield return new WaitForSeconds(fadeWaitTime);
- yield return fader.FadeIn(fadeInTime);
- Destroy(gameObject);
- }
- private void UpdatePlayer(Portal otherPortal)
- {
- GameObject player = GameObject.FindWithTag("Player");
- player.GetComponent<NavMeshAgent>().Warp(otherPortal.spawnPoint.position);
- player.transform.rotation = otherPortal.spawnPoint.rotation;
- }
- private Portal GetOtherPortal()
- {
- foreach(Portal portal in FindObjectsOfType<Portal>())
- {
- if(portal == this) { continue; }
- if(portal.destination != destination) { continue; }
- return portal;
- }
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment