Skip to content

Commit

Permalink
fix: Network rigidbody fixes (#2050)
Browse files Browse the repository at this point in the history
* using syncInterval for client auth

* only set sync var if change is greater than Sensitivity

* fixing client auth sync

* using currentVelocity
  • Loading branch information
James-Frowen committed Jun 28, 2020
1 parent 25285b1 commit 0c30d33
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions Assets/Mirror/Components/Experimental/NetworkRigidbody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -247,6 +269,7 @@ void SendRigidBodySettings()
void CmdSendVelocity(Vector3 velocity)
{
this.velocity = velocity;
target.velocity = velocity;
}

/// <summary>
Expand All @@ -258,39 +281,51 @@ 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>
/// holds previously synced values
/// </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;
Expand Down

0 comments on commit 0c30d33

Please sign in to comment.