# Apply vertical Force then return it to Zero on release Key

Hi, im having some troubles I have a swimming style game where the player moves forward, up and down, i’m applying forces to a rigidbody in order to move the player more naturally the aceleration is working fine, the thing is when i want to move up or down the player moves but the velocity of the rigidbody remains, i mean it keeps moving vertically, but i want it to only move a little bit more when the user releases the key, not to keep moving vertically at a constant speed.

Here’s the code im applying to the movement, everything is succesfully called from FixedUpdate() and input from Update()

``````	void FixedUpdate ()
{
if (isPlayer)
{
if (up && !down)
Subir();

if (down && !up)
Bajar();

}
}

public void Subir()
{
}

public void Bajar()
{
}
``````

i’m changing the up and down variables from the Input on the update. i think is not necessary to show it, the problem is if there’s some way to slowly decelerate the Y velocity of the rigidboy to Zero when the user releases the key.
I’ve already tried with different ForceMode, Impulse,Acceleration,Force, etc… without any result.

Hi, thanks, i managed to do this in another way, without directly altering the velocity of the rigidbody, it seems more natural, here’s the code:

``````             if (rigidbody.velocity.y != 0)
{
if (!up && !down)
{
if (rigidbody.velocity.y > 0)
{
}
else if (rigidbody.velocity.y < 0)
{
}
}
}
``````

anyway thanks.

As robertbu said, the general idea behind his code snippet should work, however I’d add some safety checks.

``````if (!up && !down)
{
//No point in doing this if the body is already still
if(rigidbody.velocity != 0)
{
//Multiplying by 0.9 will never result in 0, the value
//will just get smaller and smaller. So we set it to 0
//manually when its small enough. In this case 2.
if(rigidbody.velocity > 2)
rigidbody.velocity *= 0.9;
else
rigidbody.velocity = 0;
}
}
``````