- Home /
Character wont slide
I am new to unity and making scripts but I am trying to make my character slide down terrain if the angle is too steep. I am not getting any errors but it wont slide at all. Here is my script so far.
var slideDirection : Vector3; //direction of player slide var moveDirection : Vector3 = Vector3.zero; var canAngleSlide : boolean = true; var speedSlide : float = - 0.0; var moveSpeed : float = - 0.0; var slideControllableSpeed : float = 5.0; //speed at where player still has control var slideThreshhold : float = 0.88; //amount of angle for when the player slides var slideTag : String = "slide"; //the tag for when the player will slide @script RequireComponent (CharacterController) function Start () {
}
function Update () {
}
function AngleSlide(){ if (canAngleSlide) //toggle can angle slide { slideDirection = Vector3.zero; //
var hitInfo : RaycastHit; //store hit information
if (Physics.Raycast(transform.position, Vector3.down, hitInfo)) //cast a ray and find objects position and what it hit
{
if(hitInfo.collider.tag != slideTag) //if the collider tag name is tagged with slide then exit out
{
return; //return home
}
if (hitInfo.normal.y < slideThreshhold) //if our normal direction on that slope is lessthen slide threshhold
{
slideDirection = new Vector3(hitInfo.normal.x, 0, hitInfo.normal.z);//the direction for the player to slide, y is set to 0 to keep play
}
}
if (slideDirection.magnitude < slideControllableSpeed) //checks against slide speed, if less then allow some control
{
moveDirection += slideDirection; //this allows for a bit of jerky movement cleanup
}
else
{
moveDirection = slideDirection; //force the slide to go straght down based on slide direction
}
if (slideDirection.magnitude > 0) //if player is sliding
{
moveSpeed = speedSlide; //move player down at slide speed
}
}
}