From fbd23c0cce0ade38b760d648af72d01d71cf9075 Mon Sep 17 00:00:00 2001 From: in0finite Date: Wed, 1 Jul 2020 23:32:40 +0200 Subject: [PATCH] create NetworkRigidBody script --- Assets/Scripts/Networking/NetworkRigidBody.cs | 122 ++++++++++++++++++ .../Networking/NetworkRigidBody.cs.meta | 11 ++ 2 files changed, 133 insertions(+) create mode 100644 Assets/Scripts/Networking/NetworkRigidBody.cs create mode 100644 Assets/Scripts/Networking/NetworkRigidBody.cs.meta diff --git a/Assets/Scripts/Networking/NetworkRigidBody.cs b/Assets/Scripts/Networking/NetworkRigidBody.cs new file mode 100644 index 00000000..c72ceec8 --- /dev/null +++ b/Assets/Scripts/Networking/NetworkRigidBody.cs @@ -0,0 +1,122 @@ +using Mirror; +using UnityEngine; + +namespace SanAndreasUnity.Net +{ + + public class NetworkRigidBody : NetworkBehaviour + { + public Rigidbody Rigidbody { get; set; } + + Vector3 m_lastPosition = Vector3.zero; + Vector3 m_lastRotation = Vector3.zero; + Vector3 m_lastVelocity = Vector3.zero; + Vector3 m_lastAngularVelocity = Vector3.zero; + + + void Awake() + { + this.Rigidbody = this.GetComponent(); + } + + public override void OnDeserialize(NetworkReader reader, bool initialState) + { + if (null == this.Rigidbody) + return; + + byte bitfield = reader.ReadByte(); + + if ((bitfield & 1) != 0) + { + this.Rigidbody.MovePosition(reader.ReadVector3()); + } + + if ((bitfield & 2) != 0) + { + this.Rigidbody.MoveRotation(Quaternion.Euler(reader.ReadVector3())); + } + + if ((bitfield & 4) != 0) + { + this.Rigidbody.velocity = reader.ReadVector3(); + } + + if ((bitfield & 8) != 0) + { + this.Rigidbody.angularVelocity = reader.ReadVector3(); + } + + } + + public override bool OnSerialize(NetworkWriter writer, bool initialState) + { + if (null == this.Rigidbody) + return false; + + Vector3 pos = this.Rigidbody.position; + Vector3 rot = this.Rigidbody.rotation.eulerAngles; + Vector3 vel = this.Rigidbody.velocity; + Vector3 angVel = this.Rigidbody.angularVelocity; + + if (initialState) + { + writer.Write((byte) byte.MaxValue); + writer.Write(pos); + writer.Write(rot); + writer.Write(vel); + writer.Write(angVel); + + m_lastPosition = pos; + m_lastRotation = rot; + m_lastVelocity = vel; + m_lastAngularVelocity = angVel; + + return true; + } + + int startingWriterPosition = writer.Position; + writer.Write((byte)0); + + byte bitfield = 0; + + if (pos != m_lastPosition) + { + m_lastPosition = pos; + writer.Write(pos); + bitfield |= 1; + } + + if (rot != m_lastRotation) + { + m_lastRotation = rot; + writer.Write(rot); + bitfield |= 2; + } + + if (vel != m_lastVelocity) + { + m_lastVelocity = vel; + writer.Write(vel); + bitfield |= 4; + } + + if (angVel != m_lastAngularVelocity) + { + m_lastAngularVelocity = angVel; + writer.Write(angVel); + bitfield |= 8; + } + + int endWriterPosition = writer.Position; + + writer.Position = startingWriterPosition; + writer.Write(bitfield); + + writer.Position = endWriterPosition; + + return bitfield != 0; // is dirty + } + + } + +} diff --git a/Assets/Scripts/Networking/NetworkRigidBody.cs.meta b/Assets/Scripts/Networking/NetworkRigidBody.cs.meta new file mode 100644 index 00000000..2f55d57f --- /dev/null +++ b/Assets/Scripts/Networking/NetworkRigidBody.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 18b3d5884b734d84894d176734f1de9d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: