Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- public class TimeManager : MonoBehaviour {
- [SerializeField]
- private List<timeInstance> times;
- private bool isRewinding;
- private Rigidbody rb;
- public bool destroyOnStartAgain;
- public bool backToObjectOnStartAgain;
- public megacube objectToGoBackTo;
- public bool clearOnEnable;
- private Vector3 VelAtStopRewind;
- private bool justSpawned = true;
- public bool getIsRewinding()
- {
- return isRewinding;
- }
- // Use this for initialization
- void OnEnable()
- {
- if (clearOnEnable)
- {
- setNotRewinding();
- justSpawned = true;
- times = new List<timeInstance>();
- if (transform.GetComponent<Rigidbody>() != null)
- {
- rb = transform.GetComponent<Rigidbody>();
- }
- }
- }
- void Start()
- {
- if (!clearOnEnable)
- {
- setNotRewinding();
- justSpawned = true;
- times = new List<timeInstance>();
- if (transform.GetComponent<Rigidbody>() != null)
- {
- rb = transform.GetComponent<Rigidbody>();
- }
- }
- }
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.Return))
- {
- setIsRewinding();
- }
- if (Input.GetKeyUp(KeyCode.Return))
- {
- setNotRewinding();
- }
- justSpawned = false;
- }
- void FixedUpdate()
- {
- if (isRewinding)
- {
- rewind1step();
- } else
- {
- addToTimes();
- }
- }
- void setIsRewinding()
- {
- isRewinding = true;
- if (rb != null)
- {
- rb.isKinematic = true;
- }
- }
- void setNotRewinding()
- {
- isRewinding = false;
- if (rb != null)
- {
- rb.isKinematic = false;
- rb.velocity = VelAtStopRewind;
- }
- }
- void addToTimes()
- {
- if (rb != null)
- {
- times.Insert(0, new timeInstance(transform.localPosition, transform.localRotation, rb.velocity));
- }
- else
- {
- times.Insert(0, new timeInstance(transform.localPosition, transform.localRotation));
- }
- }
- void rewind1step()
- {
- if (times.Count < 1 && !justSpawned)
- {
- //setNotRewinding();
- if (destroyOnStartAgain)
- {
- GameObject.Destroy(gameObject);
- }
- if (backToObjectOnStartAgain)
- {
- gameObject.GetComponent<BoxCollider>().enabled = false;
- gameObject.GetComponent<Rigidbody>().isKinematic = true;
- transform.parent = objectToGoBackTo.transform;
- if (!objectToGoBackTo.transform.GetComponent<TimeManager>().enabled)
- {
- objectToGoBackTo.enabled = true;
- objectToGoBackTo.transform.GetComponent<TimeManager>().enabled = true;
- objectToGoBackTo.transform.GetComponent<Rigidbody>().isKinematic = false;
- objectToGoBackTo.transform.GetComponent<BoxCollider>().enabled = true;
- }
- this.enabled = false;
- }
- return;
- }
- timeInstance temp = times[0];
- times.RemoveAt(0);
- transform.localPosition = temp.position;
- transform.localRotation = temp.rotation;
- if (rb != null)
- {
- VelAtStopRewind = temp.velocity;
- }
- }
- }
- [Serializable]
- public struct timeInstance
- {
- public Vector3 position;
- public Quaternion rotation;
- public Vector3 velocity;
- public timeInstance(Vector3 _pos, Quaternion _rot, Vector3 _velocity)
- {
- position = _pos;
- rotation = _rot;
- velocity = _velocity;
- }
- public timeInstance(Vector3 _pos, Quaternion _rot)
- {
- position = _pos;
- rotation = _rot;
- velocity = new Vector3(0,0,0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement