NetworkRigidbody2D - syntax

This commit is contained in:
MrGadget1024 2022-12-22 02:41:38 -05:00
parent bcd9621a89
commit 6bb396ef5b

View File

@ -22,7 +22,6 @@ public class NetworkRigidbody2D : NetworkBehaviour
[Tooltip("Only Syncs Value if distance between previous and current is great than sensitivity")] [Tooltip("Only Syncs Value if distance between previous and current is great than sensitivity")]
[SerializeField] float velocitySensitivity = 0.1f; [SerializeField] float velocitySensitivity = 0.1f;
[Header("Angular Velocity")] [Header("Angular Velocity")]
[Tooltip("Syncs AngularVelocity every SyncInterval")] [Tooltip("Syncs AngularVelocity every SyncInterval")]
[SerializeField] bool syncAngularVelocity = true; [SerializeField] bool syncAngularVelocity = true;
@ -41,13 +40,11 @@ public class NetworkRigidbody2D : NetworkBehaviour
void OnValidate() void OnValidate()
{ {
if (target == null) if (target == null)
{
target = GetComponent<Rigidbody2D>(); target = GetComponent<Rigidbody2D>();
} }
}
#region Sync vars #region Sync vars
[SyncVar(hook = nameof(OnVelocityChanged))] [SyncVar(hook = nameof(OnVelocityChanged))]
Vector2 velocity; Vector2 velocity;
@ -69,7 +66,6 @@ void OnValidate()
/// <summary> /// <summary>
/// Ignore value if is host or client with Authority /// Ignore value if is host or client with Authority
/// </summary> /// </summary>
/// <returns></returns>
bool IgnoreSync => isServer || ClientWithAuthority; bool IgnoreSync => isServer || ClientWithAuthority;
bool ClientWithAuthority => clientAuthority && isOwned; bool ClientWithAuthority => clientAuthority && isOwned;
@ -82,7 +78,6 @@ void OnVelocityChanged(Vector2 _, Vector2 newValue)
target.velocity = newValue; target.velocity = newValue;
} }
void OnAngularVelocityChanged(float _, float newValue) void OnAngularVelocityChanged(float _, float newValue)
{ {
if (IgnoreSync) if (IgnoreSync)
@ -122,33 +117,25 @@ void OnAngularDragChanged(float _, float newValue)
target.angularDrag = newValue; target.angularDrag = newValue;
} }
#endregion
#endregion
internal void Update() internal void Update()
{ {
if (isServer) if (isServer)
{
SyncToClients(); SyncToClients();
}
else if (ClientWithAuthority) else if (ClientWithAuthority)
{
SendToServer(); SendToServer();
} }
}
internal void FixedUpdate() internal void FixedUpdate()
{ {
if (clearAngularVelocity && !syncAngularVelocity) if (clearAngularVelocity && !syncAngularVelocity)
{
target.angularVelocity = 0f; target.angularVelocity = 0f;
}
if (clearVelocity && !syncVelocity) if (clearVelocity && !syncVelocity)
{
target.velocity = Vector2.zero; target.velocity = Vector2.zero;
} }
}
/// <summary> /// <summary>
/// Updates sync var values on server so that they sync to the client /// Updates sync var values on server so that they sync to the client
@ -226,13 +213,10 @@ void SendVelocity()
previousValue.velocity = currentVelocity; previousValue.velocity = currentVelocity;
} }
// only update syncTime if either has changed // only update syncTime if either has changed
if (angularVelocityChanged || velocityChanged) if (angularVelocityChanged || velocityChanged)
{
previousValue.nextSyncTime = now + syncInterval; previousValue.nextSyncTime = now + syncInterval;
} }
}
[Client] [Client]
void SendRigidBodySettings() void SendRigidBodySettings()
@ -287,9 +271,7 @@ void CmdSendVelocityAndAngular(Vector2 velocity, float angularVelocity)
if (syncVelocity) if (syncVelocity)
{ {
this.velocity = velocity; this.velocity = velocity;
target.velocity = velocity; target.velocity = velocity;
} }
this.angularVelocity = angularVelocity; this.angularVelocity = angularVelocity;
target.angularVelocity = angularVelocity; target.angularVelocity = angularVelocity;