fix: Network rigidbody fixes (#2050)

* using syncInterval for client auth

* only set sync var if change is greater than Sensitivity

* fixing client auth sync

* using currentVelocity
This commit is contained in:
James Frowen 2020-06-28 23:46:43 +01:00 committed by GitHub
parent 25285b1574
commit 0c30d3398a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -158,16 +158,27 @@ internal void FixedUpdate()
[Server]
void SyncToClients()
{
if (syncVelocity)
// only update if they have changed more than Sensitivity
Vector3 currentVelocity = syncVelocity ? target.velocity : default;
Vector3 currentAngularVelocity = syncAngularVelocity ? target.angularVelocity : default;
bool velocityChanged = syncVelocity && ((previousValue.velocity - currentVelocity).sqrMagnitude > velocitySensitivity * velocitySensitivity);
bool angularVelocityChanged = syncAngularVelocity && ((previousValue.angularVelocity - currentAngularVelocity).sqrMagnitude > angularVelocitySensitivity * angularVelocitySensitivity);
if (velocityChanged)
{
velocity = target.velocity;
velocity = currentVelocity;
previousValue.velocity = currentVelocity;
}
if (syncAngularVelocity)
if (angularVelocityChanged)
{
angularVelocity = target.angularVelocity;
angularVelocity = currentVelocity;
previousValue.angularVelocity = currentAngularVelocity;
}
// other rigidbody settings
isKinematic = target.isKinematic;
useGravity = target.useGravity;
drag = target.drag;
@ -193,11 +204,15 @@ void SendToServer()
[Client]
void SendVelocity()
{
float now = Time.time;
if (now < previousValue.nextSyncTime)
return;
Vector3 currentVelocity = syncVelocity ? target.velocity : default;
Vector3 currentAngularVelocity = syncAngularVelocity ? target.angularVelocity : default;
bool velocityChanged = (previousValue.velocity - currentVelocity).sqrMagnitude > velocitySensitivity * velocitySensitivity;
bool angularVelocityChanged = (previousValue.angularVelocity - currentAngularVelocity).sqrMagnitude > angularVelocitySensitivity * angularVelocitySensitivity;
bool velocityChanged = syncVelocity && ((previousValue.velocity - currentVelocity).sqrMagnitude > velocitySensitivity * velocitySensitivity);
bool angularVelocityChanged = syncAngularVelocity && ((previousValue.angularVelocity - currentAngularVelocity).sqrMagnitude > angularVelocitySensitivity * angularVelocitySensitivity);
// if angularVelocity has changed it is likely that velocity has also changed so just sync both values
// however if only velocity has changed just send velocity
@ -212,6 +227,13 @@ void SendVelocity()
CmdSendVelocity(currentVelocity);
previousValue.velocity = currentVelocity;
}
// only update syncTime if either has changed
if (angularVelocityChanged || velocityChanged)
{
previousValue.nextSyncTime = now + syncInterval;
}
}
[Client]
@ -247,6 +269,7 @@ void SendRigidBodySettings()
void CmdSendVelocity(Vector3 velocity)
{
this.velocity = velocity;
target.velocity = velocity;
}
/// <summary>
@ -258,32 +281,40 @@ void CmdSendVelocityAndAngular(Vector3 velocity, Vector3 angularVelocity)
if (syncVelocity)
{
this.velocity = velocity;
target.velocity = velocity;
}
this.angularVelocity = angularVelocity;
target.angularVelocity = angularVelocity;
}
[Command]
void CmdSendIsKinematic(bool isKinematic)
{
this.isKinematic = isKinematic;
target.isKinematic = isKinematic;
}
[Command]
void CmdSendUseGravity(bool useGravity)
{
this.useGravity = useGravity;
target.useGravity = useGravity;
}
[Command]
void CmdSendDrag(float drag)
{
this.drag = drag;
target.drag = drag;
}
[Command]
void CmdSendAngularDrag(float angularDrag)
{
this.angularDrag = angularDrag;
target.angularDrag = angularDrag;
}
/// <summary>
@ -291,6 +322,10 @@ void CmdSendAngularDrag(float angularDrag)
/// </summary>
public class ClientSyncState
{
/// <summary>
/// Next sync time that velocity will be synced, based on syncInterval.
/// </summary>
public float nextSyncTime;
public Vector3 velocity;
public Vector3 angularVelocity;
public bool isKinematic;