NetworkRigidbody2D - syntax

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

View File

@ -10,7 +10,7 @@ public class NetworkRigidbody2D : NetworkBehaviour
[SerializeField] internal Rigidbody2D target = null;
[Tooltip("Set to true if moves come from owner client, set to false if moves always come from server")]
public bool clientAuthority = false;
public bool clientAuthority = false;
[Header("Velocity")]
[Tooltip("Syncs Velocity every SyncInterval")]
@ -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,32 +117,24 @@ 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>
@ -226,12 +213,9 @@ void SendVelocity()
previousValue.velocity = currentVelocity;
}
// only update syncTime if either has changed
if (angularVelocityChanged || velocityChanged)
{
previousValue.nextSyncTime = now + syncInterval;
}
}
[Client]
@ -287,9 +271,7 @@ void CmdSendVelocityAndAngular(Vector2 velocity, float angularVelocity)
if (syncVelocity)
{
this.velocity = velocity;
target.velocity = velocity;
}
this.angularVelocity = angularVelocity;
target.angularVelocity = angularVelocity;