//using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class SpaceTimeController : MonoBehaviour { List positions; public float maxSeconds; public AudioSource []rewindSFX; public GameObject invertColorMask; public VHSPostProcessEffect[] vhs; public bool isRewinding = false; public GameObject Player; public GameObject portal; public GameObject OpenedPortal; public bool PortalOpened = false; // Use this for initialization void Start () { positions = new List(); rewindSFX = GameObject.FindGameObjectWithTag("MainCamera").gameObject.GetComponents(); vhs = GameObject.FindGameObjectWithTag("MainCamera").gameObject.GetComponents(); invertColorMask = GameObject.Find("InvertColorMask"); Player = GameObject.FindGameObjectWithTag("Player"); OpenedPortal = GameObject.FindGameObjectWithTag("Portal"); } // Update is called once per frame void Update () { if (Input.GetKeyDown(KeyCode.R)) { Player.GetComponent().enabled = false; Player.GetComponent().enabled = false; Player.GetComponent().haveGun = false; Player.GetComponent().isKinematic = true; isRewinding = true; rewindSFX[1].Play(); rewindSFX[0].volume = 0; invertColorMask.GetComponent().enabled = true; //vhs[1].enabled = true; } else if(Input.GetKeyUp(KeyCode.R)) { Player.GetComponent().enabled = true; Player.GetComponent().enabled = true; Player.GetComponent().haveGun = true; Player.GetComponent().isKinematic = false; isRewinding = false; rewindSFX[1].Stop(); rewindSFX[0].volume = 1; invertColorMask.GetComponent().enabled = false; //vhs[1].enabled = false; } RewindMaster(); if (Input.GetKeyDown(KeyCode.E)) { if (OpenedPortal) { Teleport(); } else { OpenPortal(); } } } void RewindMaster() { if (isRewinding == true) { Rewind(); } else { if (GameObject.FindGameObjectWithTag("Player").GetComponent().isActive == true) { Time.timeScale = GameObject.FindGameObjectWithTag("Player").GetComponent().timeSpeed; } else { Time.timeScale = 1; } Record(); } } void Record() { if (positions.Count > Mathf.Round(maxSeconds * (1f/Time.deltaTime))) { positions.RemoveAt(positions.Count - 1); } else { positions.Insert(0,transform.position); } } void Rewind() { Time.timeScale = 2; try { this.transform.position = positions[0]; positions.RemoveAt(0); } catch (ArgumentOutOfRangeException e) { isRewinding = false; rewindSFX[1].Stop(); rewindSFX[0].volume = 1; invertColorMask.GetComponent().enabled = false; Player.GetComponent().enabled = true; Player.GetComponent().enabled = true; Player.GetComponent().haveGun = true; Player.GetComponent().isKinematic = false; if (GameObject.FindGameObjectWithTag("Player").GetComponent().isActive == true) { Time.timeScale = GameObject.FindGameObjectWithTag("Player").GetComponent().timeSpeed; } else { Time.timeScale = 1; } } } void OpenPortal() { OpenedPortal = Instantiate(portal, new Vector3(this.transform.position.x, this.transform.position.y + .1f, this.transform.position.z), this.transform.rotation); PortalOpened = true; } void Teleport() { this.transform.position = OpenedPortal.transform.position; Destroy(OpenedPortal); PortalOpened = false; } }