美文网首页
Rewind-Time

Rewind-Time

作者: RichMartin | 来源:发表于2018-01-14 17:31 被阅读25次

    游戏回放:思路通过记录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<PointInTime> pointsInTime;
    
        Rigidbody rb;
    
        // Use this for initialization
        void Start () {
            pointsInTime = new List<PointInTime>();
            rb = GetComponent<Rigidbody>();
        }
        
        // 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;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Rewind-Time

          本文链接:https://www.haomeiwen.com/subject/puzkoxtx.html