游戏回放:思路通过记录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;
}
}
网友评论