Skip to content

Commit

Permalink
Merge pull request #2 from Wully616/fixedge-raycast-distance
Browse files Browse the repository at this point in the history
Added bounding box checking to find ideal size for edge finding raycast
  • Loading branch information
Macoron authored Oct 23, 2019
2 parents b828afd + 4d60b7a commit b853857
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions Assets/Unity Simple Liquid/Scripts/SplitController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ private void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawSphere(raycasthit, 0.01f);
}
Gizmos.DrawSphere(raycastStart, 0.01f);
}
}
#endregion

Expand Down Expand Up @@ -245,6 +246,7 @@ private void SplitLogic(Vector3 splitPos)

//Used for Gizmo only
private Vector3 raycasthit;
private Vector3 raycastStart;

private void TransferLiquid(RaycastHit hit, float lostPercentAmount, float scale)
{
Expand Down Expand Up @@ -326,34 +328,52 @@ private RaycastHit FindLiquidContainer(Vector3 splitPos, GameObject ignoreCollis
return new RaycastHit();
}

#endregion
#endregion

#region Slope Logic
private Vector3 GetSlopeDirection(Vector3 up, Vector3 normal)
#region Slope Logic
private float GetIdealRayCastDist(Bounds boundBox, Vector3 point, Vector3 slope)
{
Vector3 final = boundBox.min;

// X axis
if (slope.x > 0)
final.x = boundBox.max.x;
// Y axis
if (slope.y > 0)
final.y = boundBox.max.y;
// Z axis
if (slope.z > 0)
final.z = boundBox.max.z;

return Vector3.Distance(point, final);
}

private Vector3 GetSlopeDirection(Vector3 up, Vector3 normal)
{
//https://forum.unity.com/threads/making-a-player-slide-down-a-slope.469988/#post-3062204
return Vector3.Cross(Vector3.Cross(up, normal), normal);
return Vector3.Cross(Vector3.Cross(up, normal), normal).normalized;
}

private Vector3 TryGetSlopeEdge(Vector3 slope, RaycastHit hit)
{
Vector3 edgePosition = Vector3.zero;

GameObject objHit = hit.collider.gameObject;

//flip a raycast so it faces backwards towards the object we hit, move it slightly down so it will hit the edge of the object
// We need to pick a position outside of the object to raycast back towards it to find an edge.
// We need a position slightly down so it will hit the edge of the object
Vector3 moveDown = new Vector3(0f, -0.0001f, 0f);
Vector3 reverseRayPos = hit.point + moveDown + (slope.normalized);

Ray backwardsRay = new Ray(reverseRayPos, -slope.normalized);
// We also need to move the position outside of the objects bounding box, so we actually hit it
float dist = GetIdealRayCastDist(hit.collider.bounds, hit.point, slope);

Vector3 reverseRayPos = hit.point + moveDown + (slope * dist);
raycastStart = reverseRayPos;
Ray backwardsRay = new Ray(reverseRayPos, -slope);
RaycastHit[] revHits = Physics.RaycastAll(backwardsRay);

foreach (var revHit in revHits)
{
// https://answers.unity.com/questions/752382/how-to-compare-if-two-gameobjects-are-the-same-1.html
//We only want to get this position on the original object we hit off of
if (GameObject.ReferenceEquals(revHit.collider.gameObject, objHit))
if (GameObject.ReferenceEquals(revHit.collider.gameObject, hit.collider.gameObject))
{
//We hit the object the liquid is running down!
raycasthit = edgePosition = revHit.point;
Expand All @@ -362,9 +382,9 @@ private Vector3 TryGetSlopeEdge(Vector3 slope, RaycastHit hit)
}
return edgePosition;
}
#endregion
#endregion

private void Update()
private void Update()
{
// Update bottleneck and surface from last update
bottleneckPlane = GenerateBottleneckPlane();
Expand Down

0 comments on commit b853857

Please sign in to comment.