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 {
- private List<timeInstance> times;
- private bool isRewinding;
- private Rigidbody rb;
- private Vector3 VelAtStopRewind;
- // Use this for initialization
- void Start()
- {
- 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();
- }
- }
- 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)
- {
- setNotRewinding();
- 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