Hello,
I am working on a 2D platformer and i use Rigidbody2D.velocity to move. It is easier to stop the player if i am not pressing a key and removes acceleration.
But i wanted to push the player back when there is a collision with an enemy and because the velocity is set to 0 when no key is pressed, i can’t use “AddForce”, the player stops in air. So i was wondering if there is a solution to keep make the player stop instantly but keep the AddForce.
Here is my code :
void Movement()
{
//Move
float horizontal = Input.GetAxis("Horizontal");
rb2D.velocity = new Vector2(horizontal * speed, rb2D.velocity.y);
//Jump
rb2D.AddForce(new Vector2(0, jumpForce), ForceMode.Impulse);
//Getting pushed back by enemy (debug)
if(Input.GetKeyDown(KeyCode.T))
rb2D.AddForce(new Vector2(-pushForce, 0)ForceMode.Impulse);
}