Attempt to improve time
This commit is contained in:
@@ -17,91 +17,74 @@ public class TestBall : SyncedObject
|
||||
|
||||
private float amountUp = 0;
|
||||
private int loopOffset = 0;
|
||||
private uint lastUpdate;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
networkManager.Initialize();
|
||||
lastUpdate = networkManager.CurrentTime;
|
||||
}
|
||||
|
||||
public void FixedUpdate()
|
||||
public override void FixedUpdate()
|
||||
{
|
||||
var currentTime = networkManager.CurrentTime;
|
||||
float dt = networkManager.GetDtInSeconds(currentTime, lastUpdate);
|
||||
DeltaUp(-DownPerSecond * dt);
|
||||
DeltaUp(-DownPerSecond * Dt);
|
||||
|
||||
float progress = GetProgress(currentTime);
|
||||
transform.position = Vector3.Lerp(start.position, end.position, progress) + amountUp * MaxUp * Vector3.up;
|
||||
|
||||
lastUpdate = currentTime;
|
||||
float progress = GetProgress();
|
||||
transform.position = Vector3.Lerp(start.position, end.position, progress) + amountUp * MaxUp * Vector3.up;;
|
||||
}
|
||||
|
||||
private void DeltaUp(float delta)
|
||||
{
|
||||
amountUp = Mathf.Clamp(amountUp + delta, 0, 1);
|
||||
//Debug.Log($"delta: {delta}, amountUp: {amountUp}");
|
||||
}
|
||||
|
||||
private void SetProgress(float progress, uint currentTime)
|
||||
private void SetProgress(float progress)
|
||||
{
|
||||
//loopOffset = (int)(progress * LoopTimeMs % (LoopTimeMs + currentTime));
|
||||
loopOffset = (int)(currentTime - progress * LoopTimeMs);
|
||||
//loopOffset = (int)currentTime % LoopTimeMs + (int)(progress * LoopTimeMs);
|
||||
var currentTimestamp = networkManager.GetTimestamp(networkManager.CurrentTimeTicks);
|
||||
loopOffset = (int)(currentTimestamp - progress * LoopTimeMs);
|
||||
}
|
||||
|
||||
private float GetProgress(uint currentTime)
|
||||
private float GetProgress()
|
||||
{
|
||||
return ((int)currentTime - loopOffset) % LoopTimeMs / (float)LoopTimeMs; // "uint % int" is not exposed, I love working in Udon
|
||||
//return loopOffset - ((int)currentTime % LoopTimeMs) / (float)LoopTimeMs; // "uint % int" is not exposed, I love working in Udon
|
||||
var currentTimestamp = networkManager.GetTimestamp(networkManager.CurrentTimeTicks);
|
||||
return ((int)currentTimestamp - loopOffset) % LoopTimeMs / (float)LoopTimeMs; // "uint % int" is not exposed, I love working in Udon
|
||||
}
|
||||
|
||||
public void UpButtonPressed()
|
||||
{
|
||||
DeltaUp(UpPerPress);
|
||||
Debug.Log($"({nameof(TestBall)}) Up button pressed, jumped up at {GetProgress(networkManager.CurrentTime)} to {amountUp}.");
|
||||
Debug.Log($"({nameof(TestBall)}) Up button pressed, jumped up at {GetProgress()} to {amountUp}.");
|
||||
networkManager.SendEvent((NetworkEventType)1);
|
||||
}
|
||||
|
||||
public void SyncButtonPressed()
|
||||
{
|
||||
networkManager.SendEvent((NetworkEventType)0);
|
||||
Debug.Log($"({nameof(TestBall)}) Sync button pressed, synced at progress {GetProgress(networkManager.CurrentTime)} and amountUp {amountUp}.");
|
||||
Debug.Log($"({nameof(TestBall)}) Sync button pressed, synced at progress {GetProgress()} and amountUp {amountUp}.");
|
||||
}
|
||||
|
||||
public override void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType, uint eventTime)
|
||||
public override void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType)
|
||||
{
|
||||
var currentTime = networkManager.CurrentTime;
|
||||
|
||||
if (eventType == 0)
|
||||
{
|
||||
data[index++] = BitConverter.GetBytes(amountUp);
|
||||
data[index++] = BitConverter.GetBytes(GetProgress(currentTime));
|
||||
data[index++] = BitConverter.GetBytes(GetProgress());
|
||||
}
|
||||
}
|
||||
|
||||
public override bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType, uint eventTime)
|
||||
public override bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType)
|
||||
{
|
||||
var currentTime = networkManager.CurrentTime;
|
||||
|
||||
if (eventType == 0)
|
||||
{
|
||||
amountUp = BitConverter.ToSingle(data, index);
|
||||
SetProgress(BitConverter.ToSingle(data, index + 4), currentTime);
|
||||
Debug.Log($"({nameof(TestBall)}) Received sync event, synced to progress {GetProgress(currentTime)} and amountUp {amountUp}.");
|
||||
SetProgress(BitConverter.ToSingle(data, index + 4));
|
||||
Debug.Log($"({nameof(TestBall)}) Received sync event, synced to progress {GetProgress()} and amountUp {amountUp}.");
|
||||
index += 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
DeltaUp(UpPerPress);
|
||||
Debug.Log($"({nameof(TestBall)}) Received up event, jumped up at {GetProgress(networkManager.CurrentTime)} to {amountUp}.");
|
||||
Debug.Log($"({nameof(TestBall)}) Received up event, jumped up at {GetProgress()} to {amountUp}.");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void SyncedToNewTime(uint oldTime, uint newTime)
|
||||
{
|
||||
lastUpdate += newTime - oldTime;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user