- Home /
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()
{
rigidbody.AddForce(Vector3.up * VerticalVelocity,ForceMode.Force);
}
public void Bajar()
{
rigidbody.AddForce(Vector3.down * VerticalVelocity,ForceMode.Force);
}
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.
You can handle this a couple of different ways. The easiest would be to up the drag (default is 0) in the inspector for the rigidbody. You can also directly manipulate the rigidbody.velocity. Something like:
if (!up && !down)
rigidbody.velocity *= 0.9;
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)
{
rigidbody.AddForce(new Vector3(0, -rigidbody.velocity.y*2f, 0));
}
else if (rigidbody.velocity.y < 0)
{
rigidbody.AddForce(new Vector3(0, rigidbody.velocity.y*-2f, 0));
}
}
}
anyway thanks.
I'm glad you solved your problem, though I don't get it. You add/remove force to the y axis velocity at a value of the x axis velocity? O.o ANYWHO, if your problem is gone, mark the question as closed on your first post :) Cheers!
LOL! I just made a mistake there it was rigidbody.velocity.y! not X! but it works
That was actually the final code, thanks! and how can i close the question?
Answer by Noztradamuz · Jun 26, 2013 at 04:20 PM
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)
{
rigidbody.AddForce(new Vector3(0, -rigidbody.velocity.y*2f, 0));
}
else if (rigidbody.velocity.y < 0)
{
rigidbody.AddForce(new Vector3(0, rigidbody.velocity.y*-2f, 0));
}
}
}
anyway thanks.
Answer by Em3rgency · Jun 26, 2013 at 07:07 AM
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;
}
}