# Why is my player not giving a knock back when it stays at the right side of the enemy?

So, I have this problem I am facing, for some reason my player does not get a knockback when it stays at the right side of the enemy but when it stays at the left side it gives a knockback. What's the problem?

PLAYER MOVEMENT

void Movement()
{

//THIS IS FOR WALKING
if (isControlesEnabled)
{
moveInput = Input.GetAxisRaw("Horizontal") * runSpeed;
myBody.velocity = new Vector2(moveInput, myBody.velocity.y);
}


PLAYER KNOCKBACK

private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
health -= 0;

if (health <= 0)
{
gameObject.SetActive(false);
}
else
{
ContactPoint2D contactPoint = collision.GetContact(0);
Vector2 playerPosition = transform.position;
Vector2 dir = contactPoint.point - playerPosition;

dir = -dir.normalized;

myBody.velocity = new Vector2(0, 0);
myBody.inertia = 0;

isControlesEnabled = false;
Invoke("EnablePlayerControles", controlesDisablePeriod);

}
}
}

private void EnablePlayerControles()
{
isControlesEnabled = true;
}

• Try this line of code: "myBody.velocity = dir * knockBackForce;" and comment out "myBody.velocity = dir * knockBackForce;" and check if it works. As both of them work correctly with in my project. – Swati Nov 6 '19 at 9:44
• sorry my mistake change " myBody.AddForce(dir * knockBackForce, knockBackForceMode);" of code with "myBody.velocity = dir * knockBackForce;". – Swati Nov 6 '19 at 11:54
• It's still not working tho D: could this be the assets problem or something? – Saidus Nov 6 '19 at 12:02
• Will to be honest i have no idea why its happening. But there is another thing that I think of is that the player got knocked in wrong direction.I will conform this after office time as currently i am in office so i will do it in my free time and i will let know when its done. Also another thing that might help is record "Scene View" video in which transform Arrows of the player is visible. – Swati Nov 6 '19 at 12:11
• No worries, I can wait. I also recorded "Scene View". I edited the gif above. – Saidus Nov 6 '19 at 12:34

Do your entities both use box colliders?

When two axis-aligned box colliders collide, then there isn't really a single collision point but rather a collision line on which the colliders touch. Unity is supposed to return multiple collision points in such a situation. That's why Collision2d.contacts is an array and not a single value. But your code only cares about the very first collision point in that array (ContactPoint2D contactPoint = collision.GetContact(0);).

That point happens to be the lowest point on left collisions and the highest point on right collisions. So a left-collision launches your player upwards (when it should get launched horizontally) while a right-collision launches the player downwards. The downwards trajectory pushes the player into the ground where friction kills most of its momentum, so it seems like right-collisions do almost nothing at all.

How do you fix that problem?

Instead of comparing the player position with the contact point, compare the player position with the center of the collider.

else
{
Vector2 enemyPosition = collision.transform.position;
Vector2 playerPosition = transform.position;
Vector2 dir = enemyPosition  - playerPosition;


When the positions of both objects are on the same height then you will get a horizontal motion. If you would like to maintain the slight upwards trajectory (I personally think it's quite neat), subtract a little from the height of the enemy position. This example subtracts 0.5 units. You might have to experiment a bit with the value to find the one which gives you the best results:

Vector2 enemyPosition = collision.transform.position - new Vector3(0.0f, 0.5f);


I am looking forward to playing your game.

• I would love to add the upwards trajectory. It adds more features to the game. What do you mean by "subtracting little from the z-axis"? Thanks, I hope I can create my first complete game! – Saidus Nov 6 '19 at 13:19
• @Saidus Answer updated. – Philipp Nov 6 '19 at 13:27
• The code line you gave gives an error for some reason. – Saidus Nov 6 '19 at 13:31
• @Saidus What error? – Philipp Nov 6 '19 at 13:32
• Nvm, I was able to fix it. The "- new vector" shouldn't be two but three. – Saidus Nov 6 '19 at 13:33