How to tell how recently a button was pressed

So Im making a game about fast platforming and im kinda new to this whole thing. in my game your character accelerates incredibly fast using the rigidbody and when you try to turn it takes a long time to slowdown and thats not what I want so I need to tel how recently A was pressed after D so I can slow my character down so he can accelerate in the other direction.

Going to need some information about your implementation. For now can I assume you’re using Addforce?

I would recommend instead directly setting the velocity directly to control the transition of velocity from current x velocity to target x velocity. Using forces on a rigidbody takes away a lot of control from you and can lead to situations like they where you’re trying to work around it. You shouldn’t need to try to log when a button was pressed, the Input class can tell you on the exact frame it’s pressed to you can start your transition then. you can get good results but I find the work it takes to make them look good usually isn’t worth it vs just using the rigidbody for gravity and collisions.

Something like this in update might work:

Vector3 right = new Vector3(1, 0, 0);
float speedModifier = 5;
loat maximumAxisVelocity = 30;

if (Input.GetKey(KeyCode.D))
{
    if (rigidbody.velocity.x < maximumAxisVelocity)
    {
        rigidbody.velocity += (right * Time.deltaTime * speedModifier);
    }
}
if (Input.GetKey(KeyCode.A))
{
    if (rigidbody.velocity.x > -maximumAxisVelocity)
    {
        rigidbody.velocity += (-right * Time.deltaTime * speedModifier);
    }

What it does is:

  • Create a vector that represents one unit to the right.
  • Create a maximum value for speed an an axis.
  • Create a multiplier you can use to change the rate at which the velocity is added towards the right.
  • Check the input key to go right.
  • If you aren’t already at or above the max speed in the x axis in that direction then add the unit vector
    to the right to the velocity * deltaTime (i.e. the time between frames) and multiply it by the speed
    variable.
  • Do the same but opposite for to the left. You can create your own left vector instead of using -right if that is easier to understand.

If you’re using Input.GetAxis or GetButton that’s better than this but since you said when you press D I went with this one as it’s simple. Aditionally you should make your speed modifier public or a serialized field so that you can modify how quickly the change of direction happens with 1 variable.

I managed to figure out a solution that worked
r being a rigidbody component (also if anybody else is checking this in the future my code already checks wether your not touching any buttons and automaticly stops you which can be done like this but just checking wether your not touching any buttons)

  if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.LeftArrow) && Input.GetKey(KeyCode.RightArrow))
        {
            while (r.velocity.magnitude != 0)
            {
                r.velocity = Vector3.ClampMagnitude(r.velocity, 0);
            }
            
        }