From 7103a93e370a507586af1f3f1701fc7375504222 Mon Sep 17 00:00:00 2001
From: MuhamadAjiW <16521119@mahasiswa.itb.ac.id>
Date: Sat, 27 Apr 2024 21:13:32 +0700
Subject: [PATCH] fix: slope detection

---
 .../Core/Player/PlayerMovementController.cs   | 39 +++++++++++--------
 1 file changed, 22 insertions(+), 17 deletions(-)

diff --git a/Assets/_Scripts/Core/Player/PlayerMovementController.cs b/Assets/_Scripts/Core/Player/PlayerMovementController.cs
index c1e92958..307015ed 100644
--- a/Assets/_Scripts/Core/Player/PlayerMovementController.cs
+++ b/Assets/_Scripts/Core/Player/PlayerMovementController.cs
@@ -69,7 +69,7 @@ public class PlayerMovementController
             player.transform.forward = vec;
         }
 
-        float raycastHeight = -0.03f;
+        float raycastHeight = -0.07f;
         bool stairFront = false;
 
         while (raycastHeight < maxStairHeight)
@@ -80,12 +80,14 @@ public class PlayerMovementController
             bool hit = Physics.Linecast(stairDetectorRear, stairDetectorFront, 1);
             if(hit)
             {
-                Debug.Log("Front bottom is blocked");
                 stairFront = true;
             }
             if(!hit)
             {
-                break;
+                if(raycastHeight > 0)
+                {
+                    break;
+                }
             }
 
             raycastHeight += 0.05f;
@@ -93,21 +95,24 @@ public class PlayerMovementController
 
         if(stairFront && raycastHeight < maxStairHeight)
         {
-            Vector3 dampPosition = Vector3.zero;
-
-            Vector3 targetPosition = player.transform.position + new Vector3(0, raycastHeight + 0.05f, 0);
-            
-            Debug.Log(player.model.Bottom);
-            if(Math.Abs(targetPosition.y - player.transform.position.y) < 0.2f)
-            {
-                Debug.Log("Peak");
-                targetPosition.x += 0.1f;
-                targetPosition.y += 0.1f;
-                player.transform.position = targetPosition;
-            }
-            else
+            if(inputZ != 0 || inputX != 0)
             {
-                player.transform.position = Vector3.SmoothDamp(player.transform.position, targetPosition, ref dampPosition, GameConfig.MOVEMENT_SMOOTHING / 4);
+                // Stair
+                if(raycastHeight > 0.1)
+                {
+                    Vector3 newPosition = player.Position;
+                    newPosition += Vector3.up * (raycastHeight + 0.05f);
+                    newPosition += player.transform.forward * 0.05f;
+                    player.transform.position = newPosition;
+                }
+                // Slope
+                else
+                {
+                    Debug.Log("Pushing");
+                    Debug.Log(player.model.Bottom);
+                    Vector3 force = (player.transform.forward + Vector3.up) * 0.1f;
+                    player.Rigidbody.AddForce(force, ForceMode.VelocityChange);
+                }
             }
         }
     }
-- 
GitLab