- Home /
How do I stop a RigidBody2D from moving after applying addForce?
So I'm making a platformer shooter, I have a Player character with movement controls, and a weapon system to shoot projectiles. When I shoot, I apply "Recoil" to the Rigidbody of the Player with the code:
private void Recoil() {
if (recoilMovement.magnitude > 0.1f)
{
rb.AddForce(recoilMovement);
}
}
public void ApplyRecoil(Vector2 recoilDirection, float recoilForce) {
recoilMovement = recoilDirection.normalized * recoilForce;
}
And I have this code to Stop the Recoil:
public void StopRecoilForce() { if (useRecoil) { player.ApplyRecoil(Vector2.one, 0f); }
}
And I have these Input checks: if (Input.GetMouseButton(0)) { Shoot(); }
if (Input.GetMouseButtonUp(0))
{
StopWeapon();
}
But when I shoot, the force continuously applies until I release the button. How can I stop the recoil when the mouse button is held down? Sorry for this question but I really couldn't figure it out on my own, I'm kinda new to the Unity stuff.
Answer by Z_Y_X · Jul 10, 2020 at 10:06 AM
You need to use "Input.GetMouseButton Down(0)" instead of "Input.GetMouseButton(0)" if you want to add force only onсe, when button is pressed.