diff --git a/UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/SlideActionSO.cs b/UOP1_Project/Assets/Scripts/Characters/StateMachine/Actions/SlideActionSO.cs index 88eec408e9..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) @@ -18,8 +24,17 @@ 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); + + // 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 + // 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