Rewind-Time

1855 ワード

ゲーム再生:transform Quaternionの逆方向を記録することによって
public class PointInTime {

    public Vector3 position;
    public Quaternion rotation;

    public PointInTime (Vector3 _position, Quaternion _rotation)
    {
        position = _position;
        rotation = _rotation;
    }

}

public class TimeBody : MonoBehaviour {

    bool isRewinding = false;

    public float recordTime = 5f;

    List pointsInTime;

    Rigidbody rb;

    // Use this for initialization
    void Start () {
        pointsInTime = new List();
        rb = GetComponent();
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(KeyCode.Return))
            StartRewind();
        if (Input.GetKeyUp(KeyCode.Return))
            StopRewind();
    }

    void FixedUpdate ()
    {
        if (isRewinding)
            Rewind();
        else
            Record();
    }

    void Rewind ()
    {
        if (pointsInTime.Count > 0)
        {
            PointInTime pointInTime = pointsInTime[0];
            transform.position = pointInTime.position;
            transform.rotation = pointInTime.rotation;
            pointsInTime.RemoveAt(0);
        } else
        {
            StopRewind();
        }
        
    }

    void Record ()
    {
        if (pointsInTime.Count > Mathf.Round(recordTime / Time.fixedDeltaTime))
        {
            pointsInTime.RemoveAt(pointsInTime.Count - 1);
        }

        pointsInTime.Insert(0, new PointInTime(transform.position, transform.rotation));
    }

    public void StartRewind ()
    {
        isRewinding = true;
        rb.isKinematic = true;
    }

    public void StopRewind ()
    {
        isRewinding = false;
        rb.isKinematic = false;
    }
}