Allow player to slide off of platform,
I have a FPS Player in Unity that I want to make slide. I came up with this solution for sliding the player off of slopes when they crouch. Slide() is called in Update().
[Range(1f, 20f)] public float slideSpeed = 8f;
private Vector3 hitPointNormal;
private float slideVelocity = 1f;
// Used to tell if we can / are forced to slide
private bool isSliding
{
get
{
// If we are crouching and touching the ground
if (isCrouching)
{
if (isGrounded)
{
// Then we check if the ground is not flat, but angled
RaycastHit hit;
if (Physics.Raycast(groundCheck.position, Vector3.down, out hit, groundDistance, groundLayer) || slideVelocity > 1)
{
hitPointNormal = hit.normal;
return Vector3.Angle(hitPointNormal, Vector3.up) != 0;
}
}
}
return false;
}
}
private bool wasSliding;
// Method used for sliding
private void Slide()
{
// If we are allowed and/or forced to slide
if (isSliding)
{
// Increase our slide velocity over time
if (wasSliding)
{
slideVelocity += 0.1f;
}
// Move the player accordingly
Vector3 move = new Vector3(hitPointNormal.x, -hitPointNormal.y, hitPointNormal.z) * slideSpeed * slideVelocity;
controller.Move(move * Time.deltaTime);
// Confirm we were sliding this frame
wasSliding = true;
}
// If we are not supposed to slide, reset everything
else
{
slideVelocity = 0f;
wasSliding = false;
}
}
The way that this solution is set up, the player will abruptly stop sliding as soon as they get to the end of a sloped platform (A floating slope). I want to allow the player to continue off of the platform with their slideVelocity. Any ideas would help greatly. Thanks :),
Your answer
Follow this Question
Related Questions
How to avoid CharacterControllers stepOffset launching my character into space? 1 Answer
How to Make the FPS Character Controller Prone 1 Answer
¿Cómo modifico Max forward speed en Unity 5.2.3? | How do I change Max forward speed in Unity 5.2.3? 1 Answer
walking on walls help 0 Answers
Problema multiplayer al agregar vehiculo 0 Answers