How to manage On button Hold and Release in New Input System

Hello everyone,
I am creating a script to accelerate bike as long as Player Holding Space Button and Stop bike when player release button. I am using new Input System and finding it really difficult to create this simple thing. I succeed to create this script, where, when player hold space button bike start acceleration but I have no idea how to stop bike when space button released:

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

public class RotateRearWheel : MonoBehaviour
{
    public JointMotor2D motor;
    private InputActionAsset controls;

    void Awake()
    {
        controls = new InputActionAsset();
    }

    void OnEnable()
    {
        controls.Enable();
    }

    void OnDisable()
    {
        controls.Disable();
    }

    public void OnSpace(InputValue value)
    {
        var hinge = GetComponent<HingeJoint2D>();
        var motor = hinge.motor;
        motor.motorSpeed = 200;
        hinge.motor = motor;
        hinge.useMotor = true;
    }
}

Please help me on completing this code.

You can get the InputAction you care about from your InputActionAsset, and then subscribe to the performed and canceled events to detect the hold and the release respectively.

To find the action: Class InputActionAsset | Input System | 1.1.1

The events to subscribe to: Class InputAction | Input System | 1.0.2

1 Like

Thank you so much for help. But I am completely new in coding :sweat_smile:. If you can please put these things on my above code? This way I think I can understand better.