赞
踩
先创建要回溯的信息类
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class TimeRecord
- {
- public Vector3 postion;
- public Quaternion rotation;
-
- public TimeRecord(Vector3 postion, Quaternion rotation)
- {
- this.postion = postion;
- this.rotation = rotation;
- }
- }
回溯时间
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class TimeRollback : MonoBehaviour
- {
- public float speed;
- public float RecordTime;
-
- bool isRollBack;
- List<TimeRecord> timeRecordList;
-
- private void Start()
- {
- timeRecordList = new List<TimeRecord>();
- }
-
-
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.Space))
- {
- isRollBack=true;
- }
-
- }
- private void FixedUpdate()
- {
- if (isRollBack)
- {
- RollBackTimeInformation();
- }
- else
- {
- RecordTimeInformation();
- transform.position += Vector3.right * speed * Time.fixedDeltaTime;
- }
- }
- //记录信息
- public void RecordTimeInformation()
- {
- //Mathf.Round用于对浮点数进行四舍五入。
- if (timeRecordList.Count >Mathf.Round( RecordTime/Time.deltaTime))
- {
- //移除最后一个元素
- timeRecordList.RemoveAt(timeRecordList.Count-1);
- }
- timeRecordList.Insert(0, new TimeRecord(transform.position, transform.rotation));
- }
- //回溯时间
- public void RollBackTimeInformation()
- {
- if(timeRecordList.Count>0)
- {
- transform.position = timeRecordList[0].postion;
- transform.rotation = timeRecordList[0].rotation;
- //移除下标为零的元素
- timeRecordList.RemoveAt(0);
- }
- else
- {
- isRollBack = false;
- }
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。