I’m currently adding the ability of the player to slide on steep surfaces. I have read several topics about that and I understand the concept. I managed to make something, that works pretty well, but I need some help on how to enhance it a little more.
function SlideOnSlope () {
var hit : RaycastHit;
if(grounded) {
sliding = false;
if (Physics.Raycast(transform.position, -Vector3.up, hit, 7)) {
if (Vector3.Angle(hit.normal, Vector3.up) > 30)
sliding = true;
}
else {
Physics.Raycast(contactPoint + Vector3.up, -Vector3.up, hit);
if (Vector3.Angle(hit.normal, Vector3.up) > 30)
sliding = true;
}
}
if(sliding){
var groundNormal = hit.normal;
slideDirection += Vector3(groundNormal.x/4, -groundNormal.y/4, groundNormal.z/4);
}
else
{
slideDirection = Vector3(0,0,0);
}
}
This is the sliding function, it raycasts downwards to see if the surface beneath the player is steep enough to start sliding and if so, than add some values to the slideDirection, after that I got this in the Update function:
controller.Move(moveDirection * Time.deltaTime);
controller.Move(slideDirection * Time.deltaTime);
Two controller.move functions (not sure if this is OK, but it works fine).
So now my first issue is this:
else
{
slideDirection = Vector3(0,0,0);
}
No matter how huge is the sliding speed, if the slope is no longer enough to slide on, the slide will suddenly stop. I did this, because if I don’t set slideDirection to 0, than my player will continue sliding on and on.
So what should I do to invert this action:
slideDirection += Vector3(groundNormal.x/4, -groundNormal.y/4, groundNormal.z/4);
Here the player keeps gains the speed smoothly as he slides down.
My second issue is for modifying the moveDirection. Now if the angle of the slope beneath the player is 30 or bigger he will start sliding down, but I can walk on a 29 degrees slope and speed wouldn’t even change, than suddenly if I step on a 30 degrees slope, my player will start immediately sliding.
So how would I go for modifying the speed acording to the slope I’m walking on ?
Thanks in advance!
