From 8e905d70ab0c2815305526374d2ecab1010fe16d Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Fri, 20 Sep 2024 07:00:24 -0400 Subject: [PATCH] feat: Unity 6 Compatibility (#3820) * fix(Rigidbody): Unity 6 Updates Unity 6 Preview auto-applies the following changes: - velocity -> linearVelocity - drag -> linearDamping - angularDrag -> angularDamping * Added Rigidbody Extensions for backwards compatibility * Revert "Added Rigidbody Extensions for backwards compatibility" This reverts commit 4f699be7d4c0a331aaa01baed6702b99e83a907a. * Backwards Compatibility * Backwards Compatibility * velocity -> linearVelocity * Backwards compatibility * fix(NetworkRigidbody2D) isKinematic -> bodyType --- .../NetworkRigidbodyReliable2D.cs | 21 ++++++++++++++ .../NetworkRigidbodyUnreliable2D.cs | 23 ++++++++++++++- .../PredictedRigidbody/PredictedRigidbody.cs | 28 +++++++++++++++++-- .../PredictedRigidbody/PredictionUtils.cs | 9 ++++++ .../BilliardsPredicted/Ball/Pockets.cs | 4 +++ .../Examples/CouchCoop/Scripts/CouchPlayer.cs | 9 +++++- Assets/Mirror/Examples/Pong/Scripts/Ball.cs | 8 ++++++ Assets/Mirror/Examples/Pong/Scripts/Player.cs | 4 +++ 8 files changed, 102 insertions(+), 4 deletions(-) diff --git a/Assets/Mirror/Components/NetworkRigidbody/NetworkRigidbodyReliable2D.cs b/Assets/Mirror/Components/NetworkRigidbody/NetworkRigidbodyReliable2D.cs index 87583322f..9872ccc91 100644 --- a/Assets/Mirror/Components/NetworkRigidbody/NetworkRigidbodyReliable2D.cs +++ b/Assets/Mirror/Components/NetworkRigidbody/NetworkRigidbodyReliable2D.cs @@ -36,7 +36,11 @@ protected override void Awake() Debug.LogError($"{name}'s NetworkRigidbody2D.target {target.name} is missing a Rigidbody2D", this); return; } +#if UNITY_6000_0_OR_NEWER + wasKinematic = rb.bodyType.HasFlag(RigidbodyType2D.Kinematic); +#else wasKinematic = rb.isKinematic; +#endif base.Awake(); } @@ -45,8 +49,13 @@ protected override void Awake() // for example, a game may run as client, set rigidbody.iskinematic=true, // then run as server, where .iskinematic isn't touched and remains at // the overwritten=true, even though the user set it to false originally. +#if UNITY_6000_0_OR_NEWER + public override void OnStopServer() => rb.bodyType = wasKinematic ? RigidbodyType2D.Kinematic : RigidbodyType2D.Dynamic; + public override void OnStopClient() => rb.bodyType = wasKinematic ? RigidbodyType2D.Kinematic : RigidbodyType2D.Dynamic; +#else public override void OnStopServer() => rb.isKinematic = wasKinematic; public override void OnStopClient() => rb.isKinematic = wasKinematic; +#endif // overwriting Construct() and Apply() to set Rigidbody.MovePosition // would give more jittery movement. @@ -69,7 +78,11 @@ void FixedUpdate() // only set to kinematic if we don't own it // otherwise don't touch isKinematic. // the authority owner might use it either way. +#if UNITY_6000_0_OR_NEWER + if (!owned) rb.bodyType = RigidbodyType2D.Kinematic; +#else if (!owned) rb.isKinematic = true; +#endif } // client only else if (isClient) @@ -81,7 +94,11 @@ void FixedUpdate() // only set to kinematic if we don't own it // otherwise don't touch isKinematic. // the authority owner might use it either way. +#if UNITY_6000_0_OR_NEWER + if (!owned) rb.bodyType = RigidbodyType2D.Kinematic; +#else if (!owned) rb.isKinematic = true; +#endif } // server only else if (isServer) @@ -92,7 +109,11 @@ void FixedUpdate() // only set to kinematic if we don't own it // otherwise don't touch isKinematic. // the authority owner might use it either way. +#if UNITY_6000_0_OR_NEWER + if (!owned) rb.bodyType = RigidbodyType2D.Kinematic; +#else if (!owned) rb.isKinematic = true; +#endif } } diff --git a/Assets/Mirror/Components/NetworkRigidbody/NetworkRigidbodyUnreliable2D.cs b/Assets/Mirror/Components/NetworkRigidbody/NetworkRigidbodyUnreliable2D.cs index 387772642..6c021f00b 100644 --- a/Assets/Mirror/Components/NetworkRigidbody/NetworkRigidbodyUnreliable2D.cs +++ b/Assets/Mirror/Components/NetworkRigidbody/NetworkRigidbodyUnreliable2D.cs @@ -37,7 +37,12 @@ protected override void Awake() Debug.LogError($"{name}'s NetworkRigidbody2D.target {target.name} is missing a Rigidbody2D", this); return; } + +#if UNITY_6000_0_OR_NEWER + wasKinematic = rb.bodyType.HasFlag(RigidbodyType2D.Kinematic); +#else wasKinematic = rb.isKinematic; +#endif base.Awake(); } @@ -46,9 +51,13 @@ protected override void Awake() // for example, a game may run as client, set rigidbody.iskinematic=true, // then run as server, where .iskinematic isn't touched and remains at // the overwritten=true, even though the user set it to false originally. +#if UNITY_6000_0_OR_NEWER + public override void OnStopServer() => rb.bodyType = wasKinematic ? RigidbodyType2D.Kinematic : RigidbodyType2D.Dynamic; + public override void OnStopClient() => rb.bodyType = wasKinematic ? RigidbodyType2D.Kinematic : RigidbodyType2D.Dynamic; +#else public override void OnStopServer() => rb.isKinematic = wasKinematic; public override void OnStopClient() => rb.isKinematic = wasKinematic; - +#endif // overwriting Construct() and Apply() to set Rigidbody.MovePosition // would give more jittery movement. @@ -70,7 +79,11 @@ void FixedUpdate() // only set to kinematic if we don't own it // otherwise don't touch isKinematic. // the authority owner might use it either way. +#if UNITY_6000_0_OR_NEWER + if (!owned) rb.bodyType = RigidbodyType2D.Kinematic; +#else if (!owned) rb.isKinematic = true; +#endif } // client only else if (isClient) @@ -82,7 +95,11 @@ void FixedUpdate() // only set to kinematic if we don't own it // otherwise don't touch isKinematic. // the authority owner might use it either way. +#if UNITY_6000_0_OR_NEWER + if (!owned) rb.bodyType = RigidbodyType2D.Kinematic; +#else if (!owned) rb.isKinematic = true; +#endif } // server only else if (isServer) @@ -93,7 +110,11 @@ void FixedUpdate() // only set to kinematic if we don't own it // otherwise don't touch isKinematic. // the authority owner might use it either way. +#if UNITY_6000_0_OR_NEWER + if (!owned) rb.bodyType = RigidbodyType2D.Kinematic; +#else if (!owned) rb.isKinematic = true; +#endif } } diff --git a/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs b/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs index 0fd5e6a69..02abda307 100644 --- a/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs +++ b/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs @@ -423,9 +423,12 @@ protected virtual bool IsMoving() => // predictedRigidbody.velocity.magnitude >= motionSmoothingVelocityThreshold || // predictedRigidbody.angularVelocity.magnitude >= motionSmoothingAngularVelocityThreshold; // faster implementation with cached ² +#if UNITY_6000_0_OR_NEWER + predictedRigidbody.linearVelocity.sqrMagnitude >= motionSmoothingVelocityThresholdSqr || +#else predictedRigidbody.velocity.sqrMagnitude >= motionSmoothingVelocityThresholdSqr || +#endif predictedRigidbody.angularVelocity.sqrMagnitude >= motionSmoothingAngularVelocityThresholdSqr; - // TODO maybe merge the IsMoving() checks & callbacks with UpdateState(). void UpdateGhosting() { @@ -583,7 +586,11 @@ void RecordState() // grab current position/rotation/velocity only once. // this is performance critical, avoid calling .transform multiple times. tf.GetPositionAndRotation(out Vector3 currentPosition, out Quaternion currentRotation); // faster than accessing .position + .rotation manually +#if UNITY_6000_0_OR_NEWER + Vector3 currentVelocity = predictedRigidbody.linearVelocity; +#else Vector3 currentVelocity = predictedRigidbody.velocity; +#endif Vector3 currentAngularVelocity = predictedRigidbody.angularVelocity; // calculate delta to previous state (if any) @@ -638,8 +645,13 @@ void ApplyState(double timestamp, Vector3 position, Quaternion rotation, Vector3 // hard snap to the position below a threshold velocity. // this is fine because the visual object still smoothly interpolates to it. // => consider both velocity and angular velocity (in case of Rigidbodies only rotating with joints etc.) - if (predictedRigidbody.velocity.magnitude <= snapThreshold && +#if UNITY_6000_0_OR_NEWER + if (predictedRigidbody.linearVelocity.magnitude <= snapThreshold && predictedRigidbody.angularVelocity.magnitude <= snapThreshold) +#else + if (predictedRigidbody.velocity.magnitude <= snapThreshold && + predictedRigidbody.angularVelocity.magnitude <= snapThreshold) +#endif { // Debug.Log($"Prediction: snapped {name} into place because velocity {predictedRigidbody.velocity.magnitude:F3} <= {snapThreshold:F3}"); @@ -652,7 +664,11 @@ void ApplyState(double timestamp, Vector3 position, Quaternion rotation, Vector3 // projects may keep Rigidbodies as kinematic sometimes. in that case, setting velocity would log an error if (!predictedRigidbody.isKinematic) { +#if UNITY_6000_0_OR_NEWER + predictedRigidbody.linearVelocity = velocity; +#else predictedRigidbody.velocity = velocity; +#endif predictedRigidbody.angularVelocity = angularVelocity; } @@ -704,7 +720,11 @@ void ApplyState(double timestamp, Vector3 position, Quaternion rotation, Vector3 // (projects may keep Rigidbodies as kinematic sometimes. in that case, setting velocity would log an error) if (!predictedRigidbody.isKinematic) { +#if UNITY_6000_0_OR_NEWER + predictedRigidbody.linearVelocity = velocity; +#else predictedRigidbody.velocity = velocity; +#endif predictedRigidbody.angularVelocity = angularVelocity; } } @@ -896,7 +916,11 @@ public override void OnSerialize(NetworkWriter writer, bool initialState) Time.deltaTime, position, rotation, +#if UNITY_6000_0_OR_NEWER + predictedRigidbody.linearVelocity, +#else predictedRigidbody.velocity, +#endif predictedRigidbody.angularVelocity);//, // DO NOT SYNC SLEEPING! this cuts benchmark performance in half(!!!) // predictedRigidbody.IsSleeping()); diff --git a/Assets/Mirror/Components/PredictedRigidbody/PredictionUtils.cs b/Assets/Mirror/Components/PredictedRigidbody/PredictionUtils.cs index 6c8892374..461e5ba88 100644 --- a/Assets/Mirror/Components/PredictedRigidbody/PredictionUtils.cs +++ b/Assets/Mirror/Components/PredictedRigidbody/PredictionUtils.cs @@ -21,8 +21,13 @@ public static void MoveRigidbody(GameObject source, GameObject destination, bool // copy all properties rigidbodyCopy.mass = original.mass; +#if UNITY_6000_0_OR_NEWER + rigidbodyCopy.linearDamping = original.linearDamping; + rigidbodyCopy.angularDamping = original.angularDamping; +#else rigidbodyCopy.drag = original.drag; rigidbodyCopy.angularDrag = original.angularDrag; +#endif rigidbodyCopy.useGravity = original.useGravity; rigidbodyCopy.isKinematic = original.isKinematic; rigidbodyCopy.interpolation = original.interpolation; @@ -40,7 +45,11 @@ public static void MoveRigidbody(GameObject source, GameObject destination, bool // projects may keep Rigidbodies as kinematic sometimes. in that case, setting velocity would log an error if (!original.isKinematic) { +#if UNITY_6000_0_OR_NEWER + rigidbodyCopy.linearVelocity = original.linearVelocity; +#else rigidbodyCopy.velocity = original.velocity; +#endif rigidbodyCopy.angularVelocity = original.angularVelocity; } diff --git a/Assets/Mirror/Examples/BilliardsPredicted/Ball/Pockets.cs b/Assets/Mirror/Examples/BilliardsPredicted/Ball/Pockets.cs index 2e8c27dd6..eb4ff2253 100644 --- a/Assets/Mirror/Examples/BilliardsPredicted/Ball/Pockets.cs +++ b/Assets/Mirror/Examples/BilliardsPredicted/Ball/Pockets.cs @@ -23,7 +23,11 @@ void OnTriggerEnter(Collider other) { Rigidbody rigidBody = predicted.predictedRigidbody; rigidBody.position = white.startPosition; +#if UNITY_6000_0_OR_NEWER + rigidBody.linearVelocity = Vector3.zero; +#else rigidBody.velocity = Vector3.zero; +#endif } // is it a read ball? diff --git a/Assets/Mirror/Examples/CouchCoop/Scripts/CouchPlayer.cs b/Assets/Mirror/Examples/CouchCoop/Scripts/CouchPlayer.cs index eee9eca85..3964a4e44 100644 --- a/Assets/Mirror/Examples/CouchCoop/Scripts/CouchPlayer.cs +++ b/Assets/Mirror/Examples/CouchCoop/Scripts/CouchPlayer.cs @@ -68,7 +68,11 @@ void Update() { if (Input.GetKey(KeyCode.Space) || Input.GetKeyDown(jumpKey)) { +#if UNITY_6000_0_OR_NEWER + rb.linearVelocity = new Vector2(rb.linearVelocity.x, jumpSpeed); +#else rb.velocity = new Vector2(rb.velocity.x, jumpSpeed); +#endif } } @@ -82,8 +86,11 @@ void Update() { movementVelocity = movementSpeed; } - +#if UNITY_6000_0_OR_NEWER + rb.linearVelocity = new Vector2(movementVelocity, rb.linearVelocity.y); +#else rb.velocity = new Vector2(movementVelocity, rb.velocity.y); +#endif } [ClientCallback] diff --git a/Assets/Mirror/Examples/Pong/Scripts/Ball.cs b/Assets/Mirror/Examples/Pong/Scripts/Ball.cs index 50515adfe..13284a78e 100644 --- a/Assets/Mirror/Examples/Pong/Scripts/Ball.cs +++ b/Assets/Mirror/Examples/Pong/Scripts/Ball.cs @@ -15,7 +15,11 @@ public override void OnStartServer() rigidbody2d.simulated = true; // Serve the ball from left player +#if UNITY_6000_0_OR_NEWER + rigidbody2d.linearVelocity = Vector2.right * speed; +#else rigidbody2d.velocity = Vector2.right * speed; +#endif } float HitFactor(Vector2 ballPos, Vector2 racketPos, float racketHeight) @@ -54,7 +58,11 @@ void OnCollisionEnter2D(Collision2D col) Vector2 dir = new Vector2(x, y).normalized; // Set Velocity with dir * speed +#if UNITY_6000_0_OR_NEWER + rigidbody2d.linearVelocity = dir * speed; +#else rigidbody2d.velocity = dir * speed; +#endif } } } diff --git a/Assets/Mirror/Examples/Pong/Scripts/Player.cs b/Assets/Mirror/Examples/Pong/Scripts/Player.cs index 4b9e3b9bb..a1c19adb6 100644 --- a/Assets/Mirror/Examples/Pong/Scripts/Player.cs +++ b/Assets/Mirror/Examples/Pong/Scripts/Player.cs @@ -13,7 +13,11 @@ void FixedUpdate() // only let the local player control the racket. // don't control other player's rackets if (isLocalPlayer) +#if UNITY_6000_0_OR_NEWER + rigidbody2d.linearVelocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * speed * Time.fixedDeltaTime; +#else rigidbody2d.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * speed * Time.fixedDeltaTime; +#endif } } }