How do I make so when button pressed my character can fly for 15 seconds, in unity 2d?

I am new to scripting and have searched google for answers but there are surprisingly not much tutorials on how to make a character fly, so I decided to ask here. And when I am already here might as well ask how to make it so character can fly only 15 seconds.

Well, what have you got so far?

I only have this movement script

public class PlayerMovement : MonoBehaviour
{
    // Speed and jumping height
    public float speed;
    public float jumpForce;
    float moveInput;

    // Checks if player touches the ground
    bool isGrounded = false;
    public Transform isGroundedChecker;
    public float checkGroundRadius;
    public LayerMask groundLayer;

    // Checks how long player has been on the air
    public float rememberGroundedFor;
    float lastTimeGrounded;

    // Makes jumping less floaty
    public float fallMultiplier = 2.5f;
    public float lowJumpMultiplier = 2f;

    // Makes double jumps
    public int defaultAdditionalJumps = 1;
    int additionalJumps;

    public float wallJumpTime = 0.2f;
    public float wallSlideSpeed = 0.3f;
    public float wallDistance = 0.5f;
    bool isWallSliding = false;
    RaycastHit2D WallCheckHit;
    float jumpTime;

    // Random
    bool isFacingRight = true;

    Rigidbody2D rb;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }


    void Update()
    {
        Move();
        Jump();
        BetterJump();
        CheckIfGrounded();
        CharacterFlip();
        WallJump();
    }

    void Move()
    {
        moveInput = Input.GetAxisRaw("Horizontal");
        float moveBy = moveInput * speed;
        rb.velocity = new Vector2(moveBy, rb.velocity.y);

    }

    void Jump()
    {

        if (Input.GetKeyDown(KeyCode.Space) && (isGrounded || Time.time - lastTimeGrounded <= rememberGroundedFor || additionalJumps > 0 || isWallSliding && (Input.GetKeyDown("space"))))
        {

            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            additionalJumps--;
        }
    }

    void CheckIfGrounded()
    {
        Collider2D colliders = Physics2D.OverlapCircle(isGroundedChecker.position, checkGroundRadius, groundLayer);

        if (colliders != null)
        {
            isGrounded = true;
            jumpTime = Time.time + wallJumpTime;
            additionalJumps = defaultAdditionalJumps;
        }
        else
        {
            if (isGrounded)
            {
                lastTimeGrounded = Time.time;
            }
            isGrounded = false;
        }
    }

    void BetterJump()
    {

        if (rb.velocity.y < 0)
        {
            rb.velocity += Vector2.up * Physics2D.gravity * (fallMultiplier - 1) * Time.deltaTime;
        }
        else if (rb.velocity.y > 0 && !Input.GetKey(KeyCode.Space))
        {
            rb.velocity += Vector2.up * Physics2D.gravity * (lowJumpMultiplier - 1) * Time.deltaTime;
        }
    }


    void CharacterFlip()
    {  //Makes player flip when the A and D or arrow keys are pressed.

        Vector3 charecterScale = transform.localScale;
        if (Input.GetAxis("Horizontal") < 0)
        {
            isFacingRight = false;
            charecterScale.x = -1;
        }
        if (Input.GetAxis("Horizontal") > 0)
        {
            isFacingRight = true;
            charecterScale.x = 1;
        }
        transform.localScale = charecterScale;
    }

    void WallJump()
    {


        if (isFacingRight)
        {

            WallCheckHit = Physics2D.Raycast(transform.position, new Vector2(wallDistance, 0), wallDistance, groundLayer);

        }

        else
        {

            WallCheckHit = Physics2D.Raycast(transform.position, new Vector2(-wallDistance, 0), wallDistance, groundLayer);
        }

        if (WallCheckHit && !isGrounded && moveInput != 0)
        {
            isWallSliding = true;
            jumpTime = Time.time + wallJumpTime;

        }

        else if (jumpTime < Time.time)
        {
            isWallSliding = false;
        }

        if (isWallSliding)
        {
            rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, wallSlideSpeed, float.MaxValue));
        }

    }
}

Well, one basic way to allow the player to fly, is to just allow them to jump without checking if there’s ground under them. You were kinda vague on what you meant by fly, but that’s one place to start.

To limit how long they can fly for, you could keep track of a flyTime variable, which you count down un your Update() loop. Upon running out of time, you go back to checking if the player isGrounded when jumping.

But that would make it like a flappy bird like flying, how to I make it smooth gliding like flying?

Well, instead of Jumping, you could check for GetKey() instead, which would return true every frame the button is pressed instead of just the first. Then, you can continuously add Y velocity to the player, instead of setting it to a static value.

Could you show it in code?

There’s so many ways to fly… I think that is what Red Panda is trying to get out of you.

Do you want it to just drift upwards during that 15 second like a helium balloon?

Do you want it to just be a jetpack (or helicopter) where you can go in any direction for 15 seconds?

Do you want it to actually generate lift so if you pull up it slows down and you stall?

All of these things are completely different and would have drastically-different costs and complexities to implement.

One easy tradeoff is to just disable gravity on the player as long as the time is still counting, then the player can move however he wants until the time ends and gravity comes back on.

Ok, thank you!