Browse Source

Slopes - also translate

Marcel Zickler 3 years ago
parent
commit
90cc768ca5
1 changed files with 17 additions and 6 deletions
  1. 17 6
      Assets/Scripts/SlopeCollider.cs

+ 17 - 6
Assets/Scripts/SlopeCollider.cs

@@ -57,23 +57,34 @@ public class SlopeCollider : MonoBehaviour
         var fwHit = DrawRay(fwContact);
         var rwHit = DrawRay(rwContact);
 
-        if (!fwHit.HasValue || !rwHit.HasValue || rwHit.Value.even && fwHit.Value.even)
+        if (!fwHit.HasValue || !rwHit.HasValue)
         {
             return;
         }
-        
+
         Debug.Log("----Slope Collider----");
         Debug.Log($"\tfwHit: {fwHit}");
         Debug.Log($"\trwHit: {rwHit}");
 
         var fw = fwHit.Value;
         var rw = rwHit.Value;
-        if (fw.distance >= THRESHOLD)
+        var fwDist = fw.distance;
+        var rwDist = rw.distance;
+        var distDif = fwDist - rwDist;
+
+        if (fwDist >= THRESHOLD && rwDist >= THRESHOLD)
+        {
+            var deltaY = fwDist <= rwDist ? fwDist : rwDist;
+            bike.Translate(0,-deltaY,0);
+            Debug.Log($"Translated {-deltaY} on y");
+        }
+        else if (fw.distance >= THRESHOLD)
         {
             var angle = Mathf.Atan(fw.distance / distRwFw) * Mathf.Rad2Deg;
             Debug.Log($"Rotating {angle} deg");
             bike.RotateAround(rearWheelContactPoint.position, rearWheelContactPoint.right, angle);
-        }else if (rw.distance >= THRESHOLD)
+        }
+        else if (rw.distance >= THRESHOLD)
         {
             var angle = Mathf.Atan(rw.distance / distRwFw) * Mathf.Rad2Deg;
             bike.RotateAround(frontWheelContactPoint.position, frontWheelContactPoint.right, -angle);
@@ -177,7 +188,7 @@ public class SlopeCollider : MonoBehaviour
             }
         }*/
     }
-    
+
 
     private SlopeHit? DrawRay(Vector3 start)
     {
@@ -196,7 +207,7 @@ public class SlopeCollider : MonoBehaviour
             //Debug.Log("Angle: " + angle);
             return new SlopeHit(hit.distance, !isUneven, angle, hit.point);
         }
-        
+
         if (Physics.Raycast(start, bike.up, out hit, 20f, layerMask))
         {
             Debug.DrawRay(start, -bike.up * hit.distance, Color.green);