How do I make sliding last for a definite number of time

I’m trying to make a Parkour FPS, and I want the sliding to simply make the player shorter for 1 second, but I don’t know how to make the sliding last a definite n. of seconds.

This is the code of the whole script, the sliding code is the last part of the code:

public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public float times = 6;

public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;

Vector3 velocity;

bool isGrounded;

void Update()
{
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    if (isGrounded && velocity.y < 0)
    {
        velocity.y = -2f;
    }
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");
    Vector3 move = transform.right * x + transform.forward * z;
    controller.Move(move * speed * Time.deltaTime);

    if (Input.GetButtonDown("Jump") && isGrounded)
    {
        velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    }

    velocity.y += gravity * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);

   if (Input.GetKeyDown(KeyCode.C) )
    {
        controller.height = 2f;
      Debug.Log(Time.time);
}
if (Time.time > times && Time.time < 6.1f)

{
controller.height = 3.8f;
}
}

You could use a coroutine to do this :slight_smile: You just call it when the button is pressed byt using “startcoroutine(Slide())”.

Something like:

    IEnumerator Slide()
    {
        if (!canSlide) //This checks to see if you are currently sliding. If you are, it wont run again.
        {
            yield break;         
        }

        canSlide = false; 

        float counter = 0f; //This is the timer. It starts at 0
        float duration = 1f; //This is the length of time the slide will happen for

        while (counter < duration) //for one second, the controller height will be 2f
        {
            counter += Time.deltaTime;
            controller.height = 2f;
            yield return null;
        }
//Afterwards, the height will be set back to normal here
        controller.height = ; //Put your regular controller height here 
        canSlide = true;     
    }