The function below is meant to make my player able to slide down a slope, while also moving along the slope if any input is detected. The problem is that, when I try to move it in the slide direction, he will exit the slide state, since he would come off the surface, which is not something I want. I want him to always stay in contact with the slope while sliding. Any ideas?
// called in FixedUpdate
private void HandleSlide() {
// Calculate the slide force direction and magnitude
Vector3 slideDirection = GetSlopeDirection();
float slideMagnitude = slideDirection.magnitude;
// Apply slide force
Vector3 movementInput = Player.InputHandler.MovementInput.ToIso().normalized;
// Manage velocity
float dotProduct = Vector3.Dot(Player.RB.velocity.normalized, GetSlopeDirection().normalized);
if (dotProduct < 0f) {
Player.RB.velocity = slideDirection * slideMagnitude * (Player.Data.SlideSpeed * .75f);
}
else if (dotProduct > 0f) {
Player.RB.velocity = (movementInput + slideDirection) * slideMagnitude * (Player.Data.SlideSpeed * 1.25f);
}
else {
Player.RB.velocity = (movementInput + slideDirection) * slideMagnitude * Player.Data.SlideSpeed;
}
// Rotate towards slide direction
Vector3 clampedSlideDirection = slideDirection;
clampedSlideDirection.y = 0f;
if (clampedSlideDirection != Vector3.zero)
Player.transform.rotation = Quaternion.LookRotation(clampedSlideDirection);
}