NetworkRigidbody - syntax

This commit is contained in:
MrGadget1024 2022-12-22 02:33:31 -05:00
parent 0f013adc43
commit faede54e14

View File

@ -13,7 +13,6 @@ public class NetworkRigidbody : NetworkBehaviour
public bool clientAuthority = false; public bool clientAuthority = false;
[Header("Velocity")] [Header("Velocity")]
[Tooltip("Syncs Velocity every SyncInterval")] [Tooltip("Syncs Velocity every SyncInterval")]
[SerializeField] bool syncVelocity = true; [SerializeField] bool syncVelocity = true;
@ -23,9 +22,7 @@ public class NetworkRigidbody : 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;
@ -43,13 +40,11 @@ public class NetworkRigidbody : NetworkBehaviour
void OnValidate() void OnValidate()
{ {
if (target == null) if (target == null)
{
target = GetComponent<Rigidbody>(); target = GetComponent<Rigidbody>();
} }
}
#region Sync vars #region Sync vars
[SyncVar(hook = nameof(OnVelocityChanged))] [SyncVar(hook = nameof(OnVelocityChanged))]
Vector3 velocity; Vector3 velocity;
@ -84,7 +79,6 @@ void OnVelocityChanged(Vector3 _, Vector3 newValue)
target.velocity = newValue; target.velocity = newValue;
} }
void OnAngularVelocityChanged(Vector3 _, Vector3 newValue) void OnAngularVelocityChanged(Vector3 _, Vector3 newValue)
{ {
if (IgnoreSync) if (IgnoreSync)
@ -124,33 +118,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 = Vector3.zero; target.angularVelocity = Vector3.zero;
}
if (clearVelocity && !syncVelocity) if (clearVelocity && !syncVelocity)
{
target.velocity = Vector3.zero; target.velocity = Vector3.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
@ -231,10 +217,8 @@ void SendVelocity()
// 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()
@ -289,10 +273,9 @@ void CmdSendVelocityAndAngular(Vector3 velocity, Vector3 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;
} }