- Home /
Sliding down a slope with a character controller
So I've been attempting to make it so that my third person controller slides down a slope when it's deemed too steep (with the slideThreshold variable) and would thus move down it based on the normals of the surface detected. However despite the surface detection it doesn't quite seem to even move. It gets the normals but nothing happens afterwards.
Here's the sliding function I'm attempting.
void Slide()
{
if(Grounded)
{
hitNormal = Vector3.zero;
}
Debug.DrawRay(transform.position + transform.up, debug * Vector3.down, Color.red);
if (Physics.Raycast(transform.position + transform.up, debug * Vector3.down, out hitInfo))
{
hitNormal = new Vector3(hitInfo.normal.x, -hitInfo.normal.y, hitInfo.normal.z);
if (Grounded)
{
hitNormal = Vector3.zero;
}
if (hitInfo.normal.y < SlideThreshold)
{
moveVector += hitNormal * slideSpeed;
}
if (hitNormal.magnitude < maxControllableSlideMag)
{
moveVector = hitNormal * slideSpeed;
}
}
}
void Gravity()
{
if (Grounded == false)
{
VerticalVelocity -= gravity * Time.deltaTime;
}
if (Grounded == true && TerminalVelocity < -1)
{
VerticalVelocity = -1;
}
}
Here's my movement script.
void Movement()
{
if(moveVector.magnitude > 1)
{
moveVector = Vector3.Normalize(moveVector);
}
CharacterCam = Camera.main.transform;
Slide();
camForward = Vector3.Scale(CharacterCam.forward, new Vector3(1, 0, 1)).normalized;
//make the movement Relative to the camera
moveVector = (InputZ * camForward + InputX * CharacterCam.right ).normalized;
//Get movespeed input based on player input
moveVector *= moveSpeed * InputAxis;
//Applies any Vertical movement
moveVector = new Vector3(moveVector.x, VerticalVelocity, moveVector.z);
Gravity();
player.Move(moveVector * Time.deltaTime);
}
And here's gravity.
void Gravity()
{
if (Grounded == false)
{
VerticalVelocity -= gravity * Time.deltaTime;
}
if (Grounded == true && TerminalVelocity < -1)
{
VerticalVelocity = -1;
}
}
Need anything else? Just ask and I'll provide
Answer by AgonyRoses · May 14, 2018 at 05:02 AM
Nevermind I fixed it myself but I just need to find a way to make the character controller not spaz from moving forwards then back whenever there's an attempt to move up the ramp.
It would be nice to add the "fixed" version of the code to your answer to make it a legit answer! :)
Agreed a lot of people need help with this, it would be nice to see the whole working script in action, instead of never $$anonymous$$d, I'm fine...bye.
Your answer
Follow this Question
Related Questions
Help getting sliding / surfing mechanics with rigidbodies. 0 Answers
How do I make my character fall off slopes grater than its slope limit? 0 Answers
CharacterController Slope Problems 0 Answers
2D Slope Sliding: how to prevent with transform.translate logic 0 Answers
CharacterController Slope Limit not working, character climbs onto everything 0 Answers