-
-
Save Arakade/bdbd1fbbf5b809ef2b9f775f3aeb1240 to your computer and use it in GitHub Desktop.
RealisticWater: Option to set Rigidbody kinematic when outside water area (so they don't drop).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/Assets/RealisticWater/Scripts/Water/Buoyancy.cs b/Assets/RealisticWater/Scripts/Water/Buoyancy.cs | |
index ff7d8cd..3524578 100644 | |
--- a/Assets/RealisticWater/Scripts/Water/Buoyancy.cs | |
+++ b/Assets/RealisticWater/Scripts/Water/Buoyancy.cs | |
@@ -1,3 +1,4 @@ | |
+using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
@@ -8,6 +9,8 @@ public class Buoyancy : MonoBehaviour | |
public bool IsConcave = false; | |
public int VoxelsLimit = 16; | |
public float WaveVelocity = 0.05f; | |
+ [SerializeField] | |
+ private bool disablePhysicsWhenNoRipples = false; | |
private const float Dampfer = 0.1f; | |
private const float WaterDensity = 1000; | |
@@ -18,6 +21,7 @@ public class Buoyancy : MonoBehaviour | |
private bool isMeshCollider; | |
private List<Vector3[]> forces; | |
private WaterRipples waterRipples; | |
+ private Rigidbody rb; | |
/// <summary> | |
@@ -52,22 +56,16 @@ public class Buoyancy : MonoBehaviour | |
{ | |
voxelHalfHeight = bounds.size.z; | |
} | |
- voxelHalfHeight /= 2 * SlicesPerAxis; | |
- // The object must have a RidigBody | |
- if (GetComponent<Rigidbody>() == null) | |
- { | |
- gameObject.AddComponent<Rigidbody>(); | |
- Debug.LogWarning(string.Format("[Buoyancy.cs] Object \"{0}\" had no Rigidbody. Rigidbody has been added.", name)); | |
- } | |
- GetComponent<Rigidbody>().centerOfMass = new Vector3(0, -bounds.extents.y * 0f, 0) + transform.InverseTransformPoint(bounds.center); | |
+ voxelHalfHeight /= 2 * SlicesPerAxis; | |
+ rb.centerOfMass = new Vector3(0, -bounds.extents.y * 0f, 0) + transform.InverseTransformPoint(bounds.center); | |
voxels = SliceIntoVoxels(isMeshCollider && IsConcave); | |
transform.rotation = originalRotation; | |
transform.position = originalPosition; | |
- float volume = GetComponent<Rigidbody>().mass / Density; | |
+ float volume = rb.mass / Density; | |
WeldPoints(voxels, VoxelsLimit); | |
@@ -237,13 +235,13 @@ public class Buoyancy : MonoBehaviour | |
k = 0f; | |
} | |
- var velocity = GetComponent<Rigidbody>().GetPointVelocity(wp); | |
- var localDampingForce = -velocity*Dampfer*GetComponent<Rigidbody>().mass; | |
+ var velocity = rb.GetPointVelocity(wp); | |
+ var localDampingForce = -velocity*Dampfer*rb.mass; | |
Vector3 force = localDampingForce + Mathf.Sqrt(k)*(normal*localArchimedesForce); | |
//Debug.DrawRay(wp, Mathf.Sqrt(k)*(WaterHit.normal*localArchimedesForce), Color.blue); | |
- GetComponent<Rigidbody>().AddForceAtPosition(force, wp); | |
+ rb.AddForceAtPosition(force, wp); | |
forces.Add(new[] {wp, force}); // For drawing force gizmos | |
} | |
@@ -270,19 +268,57 @@ public class Buoyancy : MonoBehaviour | |
foreach (var force in forces) | |
{ | |
Gizmos.DrawCube(force[0], new Vector3(gizmoSize, gizmoSize, gizmoSize)); | |
- Gizmos.DrawLine(force[0], force[0] + force[1] / GetComponent<Rigidbody>().mass); | |
+ Gizmos.DrawLine(force[0], force[0] + force[1] / rb.mass); | |
} | |
} | |
void OnTriggerEnter(Collider collidedObj) | |
{ | |
var temp = collidedObj.GetComponent<WaterRipples>(); | |
- if (temp!=null) | |
+ if (temp!=null) { | |
waterRipples=temp; | |
+ updateRigidbodyEnabled(); | |
+ } | |
+ } | |
+ | |
+ void OnTriggerExit(Collider collidedObj) | |
+ { | |
+ var temp = collidedObj.GetComponent<WaterRipples>(); | |
+ if (temp != null) // was it the WaterRipples Collider we just exited? | |
+ { | |
+ waterRipples = null; | |
+ updateRigidbodyEnabled(); | |
+ } | |
} | |
void OnEnable() | |
{ | |
+ // The object must have a RidigBody | |
+ rb = GetComponent<Rigidbody>(); | |
+ if (rb == null) | |
+ { | |
+ rb = gameObject.AddComponent<Rigidbody>(); | |
+ Debug.LogWarning(string.Format("[Buoyancy.cs] Object \"{0}\" had no Rigidbody. Rigidbody has been added.", name)); | |
+ } | |
+ | |
waterRipples = null; | |
+ updateRigidbodyEnabled(); | |
+ } | |
+ | |
+ void OnDisable() | |
+ { | |
+ waterRipples = null; | |
+ updateRigidbodyEnabled(); | |
+ } | |
+ | |
+ private void updateRigidbodyEnabled() | |
+ { | |
+ if (!disablePhysicsWhenNoRipples) | |
+ { | |
+ return; | |
+ } | |
+ | |
+ rb.isKinematic = (null == waterRipples); | |
} | |
-} | |
\ No newline at end of file | |
+ | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment