From 986b61cac2f9fa94f792fb5773f51eddc33b8531 Mon Sep 17 00:00:00 2001 From: kablouser Date: Sun, 26 Sep 2021 17:09:39 +0100 Subject: [PATCH 1/2] Fixed Player doesn't leave sliding state bug Replaced OrthoNormalize with a double cross product which is slightly more performant. And added a small push away from walls when they are too steep. --- .../StateMachine/Actions/SlideActionSO.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/SlideActionSO.cs b/UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/SlideActionSO.cs index 88eec408e9..e4d919fe8f 100644 --- a/UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/SlideActionSO.cs +++ b/UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/SlideActionSO.cs @@ -18,8 +18,19 @@ public override void OnUpdate() { float speed = -Physics.gravity.y * Protagonist.GRAVITY_MULTIPLIER * .4f; Vector3 hitNormal = _protagonist.lastHit.normal; - Vector3 slideDirection = new Vector3(hitNormal.x, -hitNormal.y, hitNormal.z); - Vector3.OrthoNormalize(ref hitNormal, ref slideDirection); + Vector3 biTangent = Vector3.Cross(hitNormal, Vector3.up); + Vector3 slideDirection = Vector3.Cross(hitNormal, biTangent); + + const float FLAT_NORMAL_THRESHOLD = 0.1f; + const float ADD_NORMAL_FACTOR = 0.1f; + // Check if the normal is close to flat, this happens when the surface is too steep + if (Mathf.Abs(Vector3.Dot(hitNormal, Vector3.up)) < FLAT_NORMAL_THRESHOLD) + { + // Moving downwards now may cause the CharacterController to get stuck + // Adding a small factor of the normal to the slideDirection fixes this issue + slideDirection += hitNormal * ADD_NORMAL_FACTOR; + slideDirection.Normalize(); + } //Trick below has been commented because it was pushing the character "into" the ground much too often, //producing a collision, which would result in the character being stuck while in the Sliding state From ecd0d835295dc5337e3a1e2ac49a08dcc337d78f Mon Sep 17 00:00:00 2001 From: kablouser Date: Sun, 26 Sep 2021 19:28:00 +0100 Subject: [PATCH 2/2] Tuned constant values --- .../Characters/StateMachine/Actions/SlideActionSO.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/SlideActionSO.cs b/UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/SlideActionSO.cs index e4d919fe8f..318690e6f4 100644 --- a/UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/SlideActionSO.cs +++ b/UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/SlideActionSO.cs @@ -7,6 +7,12 @@ public class SlideActionSO : StateActionSO { } public class SlideAction : StateAction { + /// + /// Calculated by Cos(90-3 degrees) because 3 degrees is the problem begins + /// + const float FLAT_NORMAL_THRESHOLD = 0.05233595624f; + const float ADD_NORMAL_FACTOR = 0.1f; + private Protagonist _protagonist; public override void Awake(StateMachine stateMachine) @@ -21,9 +27,7 @@ public override void OnUpdate() Vector3 biTangent = Vector3.Cross(hitNormal, Vector3.up); Vector3 slideDirection = Vector3.Cross(hitNormal, biTangent); - const float FLAT_NORMAL_THRESHOLD = 0.1f; - const float ADD_NORMAL_FACTOR = 0.1f; - // Check if the normal is close to flat, this happens when the surface is too steep + // Check if the normal is close to flat/when the surface is too steep if (Mathf.Abs(Vector3.Dot(hitNormal, Vector3.up)) < FLAT_NORMAL_THRESHOLD) { // Moving downwards now may cause the CharacterController to get stuck