private void FixedUpdate() { if (isRewinding && savedPlayerFrames.Count > 0) { int popIndex = Mathf.Clamp(savedPlayerFrames.Count - rewindSpeed, 0, savedPlayerFrames.Count - 1); SavedPlayerFrame savedPlayerFrame = savedPlayerFrames[popIndex]; for (int i = 0; i < rewindSpeed; i++) { if (savedPlayerFrames.Count == 0) { break; } savedPlayerFrames.RemoveAt(savedPlayerFrames.Count - 1); } transform.position = savedPlayerFrame.Position; transform.rotation = savedPlayerFrame.Rotation; } else if (!isRewinding) { Vector3 movement = new Vector3(horizontal, 0, vertical).normalized *Time.deltaTime *moveSpeed; characterController.Move(movement); if (Mathf.Abs(horizontal) + Mathf.Abs(vertical) > 0) { Quaternion targetRotation = Quaternion.LookRotation(movement); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.fixedDeltaTime * turnSpeed); } SavedPlayerFrame savedPlayerFrame = new SavedPlayerFrame(transform.position, transform.rotation); savedPlayerFrames.Add(savedPlayerFrame); } }
private void FixedUpdate() { if (savedPlayerFrames != null && savedPlayerFrames.Count > 0) { currentFrameIndex = Mathf.Clamp(isRewinding ? currentFrameIndex - rewindSpeed : currentFrameIndex + 1, 0, savedPlayerFrames.Count - 1); SavedPlayerFrame savedPlayerFrame = savedPlayerFrames[currentFrameIndex]; transform.position = savedPlayerFrame.Position; transform.rotation = savedPlayerFrame.Rotation; } else { enabled = false; } float movementDelta = Vector3.Magnitude(lastPosition - transform.position) / Time.fixedDeltaTime; currentSpeed = Mathf.Clamp01(Mathf.MoveTowards(currentSpeed, movementDelta, Time.deltaTime * 5)); lastPosition = transform.position; }