From 9e7a6858150187ee4c9fbf5a1df91b9e41f44327 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 28 Nov 2024 12:36:48 +0100 Subject: [PATCH] UNITY 6 compatibility improvements --- .../Scripts/Interaction/HandInteraction.cs | 12 ++++++++++++ .../Scripts/Movements/HandMovements.cs | 5 +++++ .../Scripts/Networking/NetworkingStatusUI.cs | 11 ++++++----- .../HumanoidControl/Scripts/Physics/HandPhysics.cs | 8 ++++++++ .../HumanoidControl/Scripts/Physics/RigidbodyData.cs | 10 ++++++++++ .../HumanoidControl/Scripts/Targets/HandTarget.cs | 9 +++++++++ Runtime/HumanoidControl/Scripts/Tools/Redrop.cs | 10 +++++++--- Runtime/Tools/Physics/HybridPhysics.cs | 4 ++++ Runtime/Tools/Physics/RigidbodyDisabled.cs | 10 ++++++++++ Runtime/Tools/Scripts/HumanoidSpawnPoint.cs | 12 ++++++++---- Runtime/Tools/Scripts/InteractionPointer.cs | 8 ++++++++ 11 files changed, 87 insertions(+), 12 deletions(-) diff --git a/Runtime/HumanoidControl/Scripts/Interaction/HandInteraction.cs b/Runtime/HumanoidControl/Scripts/Interaction/HandInteraction.cs index d409dc0..09b8bfa 100644 --- a/Runtime/HumanoidControl/Scripts/Interaction/HandInteraction.cs +++ b/Runtime/HumanoidControl/Scripts/Interaction/HandInteraction.cs @@ -32,7 +32,11 @@ namespace Passer.Humanoid { inputModule = humanoid.GetComponent(); if (inputModule == null) { +#if UNITY_6000_0_OR_NEWER + inputModule = Object.FindAnyObjectByType(); +#else inputModule = Object.FindObjectOfType(); +#endif if (inputModule == null) { inputModule = humanoid.gameObject.AddComponent(); } @@ -825,7 +829,11 @@ namespace Passer.Humanoid { Rigidbody objRigidbody = RigidbodyDisabled.UnparentRigidbody(handPalm, grabbedObject.transform); if (objRigidbody != null && !objRigidbody.isKinematic) { if (handRigidbody != null) { +#if UNITY_6000_0_OR_NEWER + objRigidbody.linearVelocity = handRigidbody.linearVelocity; +#else objRigidbody.velocity = handRigidbody.velocity; +#endif objRigidbody.angularVelocity = handRigidbody.angularVelocity; } HumanoidNetworking.ReenableNetworkSync(objRigidbody.gameObject); @@ -853,7 +861,11 @@ namespace Passer.Humanoid { //grabbedRigidbody.centerOfMass = handTarget.storedCOM; if (grabbedRigidbody.isKinematic == false) { +#if UNITY_6000_0_OR_NEWER + grabbedRigidbody.linearVelocity = handRigidbody.linearVelocity; +#else grabbedRigidbody.velocity = handRigidbody.velocity; +#endif grabbedRigidbody.angularVelocity = handRigidbody.angularVelocity; } diff --git a/Runtime/HumanoidControl/Scripts/Movements/HandMovements.cs b/Runtime/HumanoidControl/Scripts/Movements/HandMovements.cs index 8c795ca..7174c44 100644 --- a/Runtime/HumanoidControl/Scripts/Movements/HandMovements.cs +++ b/Runtime/HumanoidControl/Scripts/Movements/HandMovements.cs @@ -53,8 +53,13 @@ namespace Passer.Humanoid { handTarget.handRigidbody = handTarget.hand.bone.transform.gameObject.AddComponent(); } handTarget.handRigidbody.mass = 1; +#if UNITY_2017_1_OR_NEWER + handTarget.handRigidbody.linearDamping = 0; + handTarget.handRigidbody.angularDamping = 10; +#else handTarget.handRigidbody.drag = 0; handTarget.handRigidbody.angularDrag = 10; +#endif handTarget.handRigidbody.useGravity = false; handTarget.handRigidbody.isKinematic = true; handTarget.handRigidbody.interpolation = RigidbodyInterpolation.None; diff --git a/Runtime/HumanoidControl/Scripts/Networking/NetworkingStatusUI.cs b/Runtime/HumanoidControl/Scripts/Networking/NetworkingStatusUI.cs index b452e44..a5f1a6f 100644 --- a/Runtime/HumanoidControl/Scripts/Networking/NetworkingStatusUI.cs +++ b/Runtime/HumanoidControl/Scripts/Networking/NetworkingStatusUI.cs @@ -6,18 +6,19 @@ namespace Passer { protected NetworkingStarter networkingStarter; protected UnityEngine.UI.Text textcomponent; - void Start() { + protected void Start() { +#if UNITY_6000_0_OR_NEWER + networkingStarter = Object.FindAnyObjectByType(); +#else networkingStarter = FindObjectOfType(); +#endif textcomponent = GetComponent(); } - void Update() { + protected void Update() { #if hNW_UNET || hNW_PHOTON if (networkingStarter == null) return; - - //if (textcomponent != null) -// textcomponent.text = networkingStarter.networkingStatus.ToString(); #endif } } diff --git a/Runtime/HumanoidControl/Scripts/Physics/HandPhysics.cs b/Runtime/HumanoidControl/Scripts/Physics/HandPhysics.cs index 9194e02..18e9efd 100644 --- a/Runtime/HumanoidControl/Scripts/Physics/HandPhysics.cs +++ b/Runtime/HumanoidControl/Scripts/Physics/HandPhysics.cs @@ -227,7 +227,11 @@ namespace Passer.Humanoid { //Compensate for absolute rigidbody speed (specifically when on a moving platform) if (handRigidbody != null) { +#if UNITY_6000_0_OR_NEWER + Vector3 residualVelocity = handRigidbody.linearVelocity - velocityTowardsTarget; +#else Vector3 residualVelocity = handRigidbody.velocity - velocityTowardsTarget; +#endif damper += residualVelocity * 10; } } @@ -552,7 +556,11 @@ namespace Passer.Humanoid { Vector3 velocityTarget = (positionDelta * velocityMagic) * Time.fixedDeltaTime; if (float.IsNaN(velocityTarget.x) == false) +#if UNITY_6000_0_OR_NEWER + handRigidbody.linearVelocity = Vector3.MoveTowards(handRigidbody.linearVelocity, velocityTarget, MaxVelocityChange); +#else handRigidbody.velocity = Vector3.MoveTowards(handRigidbody.velocity, velocityTarget, MaxVelocityChange); +#endif rotationDelta.ToAngleAxis(out angle, out axis); diff --git a/Runtime/HumanoidControl/Scripts/Physics/RigidbodyData.cs b/Runtime/HumanoidControl/Scripts/Physics/RigidbodyData.cs index ea0d94f..b870927 100644 --- a/Runtime/HumanoidControl/Scripts/Physics/RigidbodyData.cs +++ b/Runtime/HumanoidControl/Scripts/Physics/RigidbodyData.cs @@ -15,8 +15,13 @@ namespace Passer { public RigidbodyData(Rigidbody rb) { mass = rb.mass; +#if UNITY_6000_0_OR_NEWER + drag = rb.linearDamping; + angularDrag = rb.angularDamping; +#else drag = rb.drag; angularDrag = rb.angularDrag; +#endif useGravity = rb.useGravity; isKinematic = rb.isKinematic; interpolation = rb.interpolation; @@ -28,8 +33,13 @@ namespace Passer { public void CopyToRigidbody(Rigidbody rb) { rb.mass = mass; +#if UNITY_6000_0_OR_NEWER + rb.linearDamping = drag; + rb.angularDamping = angularDrag; +#else rb.drag = drag; rb.angularDrag = angularDrag; +#endif rb.useGravity = useGravity; rb.isKinematic = isKinematic; rb.interpolation = interpolation; diff --git a/Runtime/HumanoidControl/Scripts/Targets/HandTarget.cs b/Runtime/HumanoidControl/Scripts/Targets/HandTarget.cs index c74a3f6..2d9c888 100644 --- a/Runtime/HumanoidControl/Scripts/Targets/HandTarget.cs +++ b/Runtime/HumanoidControl/Scripts/Targets/HandTarget.cs @@ -1163,7 +1163,11 @@ namespace Passer.Humanoid { private HumanoidControl GetHumanoid() { // This does not work for prefabs +#if UNITY_6000_0_OR_NEWER + HumanoidControl[] humanoids = FindObjectsByType(FindObjectsSortMode.None); +#else HumanoidControl[] humanoids = FindObjectsOfType(); +#endif for (int i = 0; i < humanoids.Length; i++) { if ((humanoids[i].leftHandTarget != null && humanoids[i].leftHandTarget.transform == this.transform) || @@ -1195,8 +1199,13 @@ namespace Passer.Humanoid { handRigidbody = hand.bone.transform.gameObject.AddComponent(); } handRigidbody.mass = 1; +#if UNITY_6000_0_OR_NEWER + handRigidbody.linearDamping = 0; + handRigidbody.angularDamping = 10; +#else handRigidbody.drag = 0; handRigidbody.angularDrag = 10; +#endif handRigidbody.useGravity = false; handRigidbody.isKinematic = true; handRigidbody.interpolation = RigidbodyInterpolation.None; diff --git a/Runtime/HumanoidControl/Scripts/Tools/Redrop.cs b/Runtime/HumanoidControl/Scripts/Tools/Redrop.cs index 43eb647..36097cb 100644 --- a/Runtime/HumanoidControl/Scripts/Tools/Redrop.cs +++ b/Runtime/HumanoidControl/Scripts/Tools/Redrop.cs @@ -12,18 +12,18 @@ namespace Passer { private Vector3 startPosition; private Quaternion startRotation; - void Start() { + protected void Start() { startPosition = transform.position; startRotation = transform.rotation; } - void Update() { + protected void Update() { if (groundCollider == null && transform.position.y < 0) { MoveToStart(); } } - private void OnCollisionEnter(Collision collision) { + protected void OnCollisionEnter(Collision collision) { if (collision.collider == groundCollider) { MoveToStart(); } @@ -34,7 +34,11 @@ namespace Passer { if (thisRigidbody != null) { thisRigidbody.MovePosition(new Vector3(startPosition.x, startPosition.y + 0.1F, startPosition.z)); thisRigidbody.MoveRotation(startRotation); +#if UNITY_6000_0_OR_NEWER + thisRigidbody.linearVelocity = Vector3.zero; +#else thisRigidbody.velocity = Vector3.zero; +#endif thisRigidbody.angularVelocity = Vector3.zero; } } diff --git a/Runtime/Tools/Physics/HybridPhysics.cs b/Runtime/Tools/Physics/HybridPhysics.cs index 951b60c..9d8f7e2 100644 --- a/Runtime/Tools/Physics/HybridPhysics.cs +++ b/Runtime/Tools/Physics/HybridPhysics.cs @@ -229,7 +229,11 @@ namespace Passer { damper = -velocityTowardsTarget * damping; //Compensate for absolute rigidbody speed (specifically when on a moving platform) +#if UNITY_6000_0_OR_NEWER + Vector3 residualVelocity = thisRigidbody.linearVelocity - velocityTowardsTarget; +#else Vector3 residualVelocity = thisRigidbody.velocity - velocityTowardsTarget; +#endif damper += residualVelocity * 10; } lastDistanceToTarget = distanceToTarget; diff --git a/Runtime/Tools/Physics/RigidbodyDisabled.cs b/Runtime/Tools/Physics/RigidbodyDisabled.cs index cacab60..31d3fe2 100644 --- a/Runtime/Tools/Physics/RigidbodyDisabled.cs +++ b/Runtime/Tools/Physics/RigidbodyDisabled.cs @@ -30,8 +30,13 @@ namespace Passer { public void CopyFromRigidbody(Rigidbody rb) { mass = rb.mass; +#if UNITY_6000_0_OR_NEWER + drag = rb.linearDamping; + angularDrag = rb.angularDamping; +#else drag = rb.drag; angularDrag = rb.angularDrag; +#endif useGravity = rb.useGravity; isKinematic = rb.isKinematic; interpolation = rb.interpolation; @@ -44,8 +49,13 @@ namespace Passer { public void CopyToRigidbody(Rigidbody rb) { rb.mass = mass; +#if UNITY_6000_0_OR_NEWER + rb.linearDamping = drag; + rb.angularDamping = angularDrag; +#else rb.drag = drag; rb.angularDrag = angularDrag; +#endif rb.useGravity = useGravity; rb.isKinematic = isKinematic; rb.interpolation = interpolation; diff --git a/Runtime/Tools/Scripts/HumanoidSpawnPoint.cs b/Runtime/Tools/Scripts/HumanoidSpawnPoint.cs index 6aa64fe..73d1ca9 100644 --- a/Runtime/Tools/Scripts/HumanoidSpawnPoint.cs +++ b/Runtime/Tools/Scripts/HumanoidSpawnPoint.cs @@ -46,13 +46,17 @@ namespace Passer.Humanoid { } } - private void Awake() { + protected void Awake() { +#if UNITY_6000_0_OR_NEWER + HumanoidControl humanoid = Object.FindAnyObjectByType(); +#else HumanoidControl humanoid = FindObjectOfType(); +#endif if (humanoid == null) return; - Vector3 humanoidXZ = new Vector3(humanoid.transform.position.x, 0, humanoid.transform.position.z); - Vector3 thisXZ = new Vector3(this.transform.position.x, 0, this.transform.position.z); + Vector3 humanoidXZ = new(humanoid.transform.position.x, 0, humanoid.transform.position.z); + Vector3 thisXZ = new(this.transform.position.x, 0, this.transform.position.z); float distance = Vector3.Distance(humanoidXZ, thisXZ); if (distance < radius || isFree) humanoid.transform.MoveTo(this.transform.position); @@ -103,6 +107,6 @@ namespace Passer.Humanoid { Gizmos.DrawLine(pos, lastPos); } - #endregion + #endregion } } diff --git a/Runtime/Tools/Scripts/InteractionPointer.cs b/Runtime/Tools/Scripts/InteractionPointer.cs index 361ab4f..2551b9f 100644 --- a/Runtime/Tools/Scripts/InteractionPointer.cs +++ b/Runtime/Tools/Scripts/InteractionPointer.cs @@ -224,7 +224,11 @@ namespace Passer { protected virtual void Awake() { Transform rootTransform = this.transform.root; +#if UNITY_6000_0_OR_NEWER + interactionModule = FindAnyObjectByType(); +#else interactionModule = FindObjectOfType(); +#endif if (interactionModule == null) interactionModule = CreateInteractionModule(); EventSystem eventSystem = interactionModule.GetComponent(); @@ -585,7 +589,11 @@ namespace Passer { if (rayType != RayType.Gravity) return; +#if UNITY_6000_0_OR_NEWER + rigidbody.linearVelocity = transform.forward * speed; +#else rigidbody.velocity = transform.forward * speed; +#endif } public void LaunchPrefab(GameObject prefab) {