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