1
votes

Im trying to make simple 3rd person character controller using unitys character controller component instead of rigidbody. I have problem when making my character sliding down the slope, the motion is jerky, just as if the character was going down stairs.

I move my character using normal to the ground by reversing its y axis, then i apply some additional gravity and put this vector to charactercontroller.move() function.

Here is some of the code where i apply slide and gravity:

void ProcessMotion(){
    MoveVector = transform.TransformDirection (MoveVector);

    if (MoveVector.magnitude > 1)
        MoveVector = Vector3.Normalize (MoveVector);

    ApplySlide ();

    MoveVector *= MoveSpeed;

    MoveVector = new Vector3 (MoveVector.x, VerticalVel, MoveVector.z);

    ApplyGravity ();



    TP_Controller.CharacterController.Move (MoveVector*Time.deltaTime);

}

public void Jump(){
    if (TP_Controller.CharacterController.isGrounded) {
        VerticalVel=jumpSpeed;
    }
}

void SnapAlignCharacterWithCamera(){
    if (MoveVector.x != 0 || MoveVector.z != 0) {
        transform.rotation = Quaternion.Euler(transform.eulerAngles.x, Camera.main.transform.eulerAngles.y, transform.eulerAngles.z);
    }
}

void ApplyGravity(){

    if (MoveVector.y > -TermVel) {

        MoveVector = new Vector3 (MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);

    } 
    if (TP_Controller.CharacterController.isGrounded && MoveVector.y < - 1) {

        MoveVector = new Vector3 (MoveVector.x, -1, MoveVector.z);

    }

}

void ApplySlide(){
    if (!TP_Controller.CharacterController.isGrounded) {

        return;
    }
    SlideDirection = Vector3.zero;

    RaycastHit hitInfo;


    if (Physics.Raycast (transform.position , Vector3.down, out hitInfo)) {

        if(hitInfo.normal.y < SlideTreshold){
            SlideDirection = new Vector3(hitInfo.normal.x, -hitInfo.normal.y, hitInfo.normal.z)*10;
        }

    }
    if (SlideDirection.magnitude < MaxMagnitude) {
        MoveVector += SlideDirection;

        //Debug.DrawLine (transform.position,transform.position + new Vector3(hitInfo.normal.x*0.5f,-hitInfo.normal.y,hitInfo.normal.z*0.5f), Color.red,1.0f);
    }else {

        MoveVector = SlideDirection;
    }

}

And here are screens with gizmos that show path of the object:
Sliding slowly
Sliding 10xfaster

In advance thanks for your help!

1
where you able to find a solution for this? im facing the same issue :(Fo Nko

1 Answers

0
votes

I'm not sure the scale of your objects, but it looks like your raycast is coming from inside of your capsule, and possibly colliding with the bottom. The resulting RaycastHit would report that information, which is a normal that points downward, so when you reverse it, you are heading upwards, creating the sawtooth effect.