Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed player sliding stuck bug #501

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ public class SlideActionSO : StateActionSO<SlideAction> { }

public class SlideAction : StateAction
{
/// <summary>
/// Calculated by Cos(90-3 degrees) because 3 degrees is the problem begins
/// </summary>
const float FLAT_NORMAL_THRESHOLD = 0.05233595624f;
const float ADD_NORMAL_FACTOR = 0.1f;

private Protagonist _protagonist;

public override void Awake(StateMachine stateMachine)
Expand All @@ -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
Expand Down