How would you code a collectible object so that the actual act of picking up the object takes a certain amount of time?

I already have functionality to pick up collectible objects using collision detection and a simple press of the e key. I was, however, hoping to make it take a set amount of time, say 3 seconds, of pressing the e key within range of the object before pick up occurred. How would I go about doing this? Thank you!

There could be multiple solutions to this, but I’d do something like following:

If the player is in close proximity, I’d decide that by using a sphere collider set as trigger. If the object is in the trigger area, then I’d check it in Update function:

void Update() {
    if (Input.GetKey("e") && isObjectInVicinity) {
        pressedTimer += Time.deltaTime;
        if( pressedTimer >= 3) {
            // pick up object
    } else {
        pressedTimer = 0f;

void OnTriggerEnter(Collider other) {
    if( == "my pick up object") {
        isObjectInVicinity = true;

Of course you could go about different things, like using Vector3.Distance function to check is object is near, or use CompareTag("object tag") function in OnTriggerEnter to check if object is pickable.

You can also set object reference to your pickable object.

There are many other ways to do this, I hope my examples give you an idea.