How would I increase my players momentum over time of a KeyDown?

Hey, I just started on a new project and would like to be able to transition from my player walking to running, but obviously I would need to increase my players speed over time for this as I do not want to use a button. Ideally I want the speed to increase to a certain amount after the Key is held down for 3 seconds. I’m still learning and have no idea how I would pull this off. Thanks for the help!

You should use a three-state system.

Use Input.GetButtonDown() to start it.

From then, count the time while held.

Then, on KeyUp, reset the timer.

For Example, here’s a code segment from one of my projects.

if(Input.GetKeyDown(b.inputKey)) // set status to down, frames held to one
                b.status = 'd';
                b.framesHeld = 1;
            else if(Input.GetKey(b.inputKey)) // set status to hold, increment frames held
                b.status = 'h';
            else if(Input.GetKeyUp(b.inputKey)) // set status to up, leave frames held
                b.status = 'u';
            else //button is untouched: set status to no input, set frames held to zero
                b.status = 'n';
                b.framesHeld = 0;

This isn’t exactly what you need, but you should be able to adapt from it.

If not, I am happy to provide a more detailed rendition of what you need.
(Hint, you’d need to count by Time.deltaTime rather than frames, and then check if it’s >3).

For increasing speed over time, consider using a Rigidbody and AddForce();

I think I roughly figured it out:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyScript : MonoBehaviour {
    public Rigidbody rb;
    public float timeHeld;
    public float accelerationRate;
    public float deaccelerationRate;
    public float speed;

    public void Update()
        speed = rb.velocity.z;
        if (Input.GetKey(KeyCode.W) && timeHeld < 2.988f)
            rb.velocity = new Vector3(0f, 0f, (rb.velocity.z + accelerationRate));
            timeHeld += 1f * Time.deltaTime;
        if (!Input.GetKey(KeyCode.W) && speed > 1f)
            timeHeld -= 1f * Time.deltaTime;
        if (rb.velocity.z > 0f && !Input.GetKey(KeyCode.W))
            rb.velocity = new Vector3(0f, 0f, (rb.velocity.z - deaccelerationRate));