|
| 1 | +using MLAPI.Data; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEngine; |
| 4 | + |
| 5 | +namespace MLAPI.MonoBehaviours.Core |
| 6 | +{ |
| 7 | + //Based on: https://twotenpvp.github.io/lag-compensation-in-unity.html |
| 8 | + //Modified to be used with latency rather than fixed frames and subframes. Thus it will be less accrurate but more modular. |
| 9 | + public class TrackedObject : MonoBehaviour |
| 10 | + { |
| 11 | + internal Dictionary<float, TrackedPointData> FrameData = new Dictionary<float, TrackedPointData>(); |
| 12 | + internal List<float> Framekeys = new List<float>() { 0 }; |
| 13 | + private Vector3 savedPosition; |
| 14 | + private Quaternion savedRotation; |
| 15 | + |
| 16 | + internal void ReverseTransform(float secondsAgo) |
| 17 | + { |
| 18 | + savedPosition = transform.position; |
| 19 | + savedRotation = transform.rotation; |
| 20 | + float currentTime = Time.time; |
| 21 | + float targetTime = Time.time - secondsAgo; |
| 22 | + float previousTime = 0; |
| 23 | + float nextTime = 0; |
| 24 | + for (int i = 1; i < Framekeys.Count - 1; i++) |
| 25 | + { |
| 26 | + if(Framekeys[i - 1] > targetTime && Framekeys[i] <= targetTime) |
| 27 | + { |
| 28 | + previousTime = Framekeys[i]; |
| 29 | + nextTime = Framekeys[i + 1]; |
| 30 | + break; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + float timeBetweenFrames = nextTime - previousTime; |
| 35 | + float timeAwayFromPrevious = currentTime - previousTime; |
| 36 | + float lerpProgress = timeAwayFromPrevious / timeBetweenFrames; |
| 37 | + transform.position = Vector3.Lerp(FrameData[previousTime].position, FrameData[nextTime].position, lerpProgress); |
| 38 | + transform.rotation = Quaternion.Slerp(FrameData[previousTime].rotation, FrameData[nextTime].rotation, lerpProgress); |
| 39 | + } |
| 40 | + |
| 41 | + internal void ResetStateTransform() |
| 42 | + { |
| 43 | + transform.position = savedPosition; |
| 44 | + transform.rotation = savedRotation; |
| 45 | + } |
| 46 | + |
| 47 | + void Start() |
| 48 | + { |
| 49 | + LagCompensationManager.SimulationObjects.Add(this); |
| 50 | + } |
| 51 | + |
| 52 | + void OnDestroy() |
| 53 | + { |
| 54 | + LagCompensationManager.SimulationObjects.Remove(this); |
| 55 | + } |
| 56 | + |
| 57 | + internal void AddFrame() |
| 58 | + { |
| 59 | + float currentTime = Time.time; |
| 60 | + for (int i = 0; i < Framekeys.Count; i++) |
| 61 | + { |
| 62 | + if (currentTime - Framekeys[i] <= NetworkingManager.singleton.NetworkConfig.SecondsHistory) |
| 63 | + { |
| 64 | + for (int j = 0; j < i; j++) |
| 65 | + { |
| 66 | + FrameData.Remove(Framekeys[0]); |
| 67 | + //This is not good for performance. Other datatypes should be concidered. |
| 68 | + Framekeys.RemoveAt(0); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + FrameData.Add(Time.time, new TrackedPointData() |
| 73 | + { |
| 74 | + position = transform.position, |
| 75 | + rotation = transform.rotation |
| 76 | + }); |
| 77 | + Framekeys.Add(Time.frameCount); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments