Unexpected force to hurt player

So when my player gets hurt, he is forced back with

rigid.velocity = new Vector2(d, 10.0f);

Sometimes when the player is hit by a moving enemy, an unexpected extra force in multiplied into the force back. Visually it’s annoying and not fun. Is there a way to have the force back at a consistent magnitude?

You could do this to force a max speed after you add forces:

rigidbody.velocity = Vector3.ClampMagnitude(rigidbody.velocity, maxSpeed);

I would recommend trying to figure out where the extra force originates from, because this solution is somewhat of a band-aid to the underlying issue perhaps.

Thanks. The unexpected force usually happen

Thanks. The unexpected force usually occurs when I’m jumping into an enemy. Maybe I should temporarily shut off enemy collision when hurt.

I think I needed to remove any velocity of the rigidbody (like from a jump) before adding the the hurt velocity

 rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = 0;

Anytime you set the velocity explicitly (not using AddForce, but doing velocity=vector), it’s effectively overwriting any velocity that was already there. So unless you were manually adding in the previous velocity, I’m not sure zeroing out the velocity would affect your result.