# How to reliably add knockback to the player and other entities?

I’m using a RigidBody-based movement system that adds velocity to the entity with a SetVelocity function. I’m making a 2D platformer, where that knockback can be added from any angle. For now I’m doing it with RigidBody’s AddForce function, but for some reason it only works normally when the knockback is added from above, or from below. if it’s added horisontally or diagonally, the effect is as if the playeris being pushed into an invisible wall, it gets pushed a bit and then stops moving horisontally at all. I suspect it might be because the function is called once and not lerped smoothly in every Update call, but I’m not sure.
Is there a better way of doing this?

You’re most likely explicitly setting the X velocity and overriding any accumulating forces. You’ll have to share your movement code.

These’re the functions called in other places whenever they’re needed. Some are called inUpdate, some are not. AddForceAtAngle is used for knockback and is called once.

``````    //Set the X velocity of the entity
public void SetVelocityX(float velocity)
{
SetFinalVelocity(new Vector2(_rigidBody.velocity.x + velocity, _rigidBody.velocity.y));
}

//Set the Y velocity of the entity
public void SetVelocityY(float velocity)
{
SetFinalVelocity(new Vector2(_rigidBody.velocity.x, velocity));
}

//Set the velocity of the entity at an angle with and considering facingDirection
public void SetVelocity(float velocity, Vector2 angle, int facingDirection)
{
angle.Normalize();
SetFinalVelocity(new Vector2(angle.x * velocity * facingDirection, angle.y * velocity));
}

//Set the velocity of the entity considering facingDirection
public void SetVelocity(float velocity, Vector2 facingDirection)
{
SetFinalVelocity(velocity * facingDirection);
}

//Set the velocity of the entity to 0
public void SetVelocityZero()
{
SetFinalVelocity(Vector2.zero);
}

//Set the velocity of the entity to workspace vector
public void SetFinalVelocity(Vector2 workspace)
{
if (_canSetVelocity)
{
_rigidBody.velocity = workspace;
}
}

public void AddForceAtAngle(float force, float angle)
{
Vector2 dir = Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.right;