# How to receive a Knock Back when the player touched the enemy

I'm still new to C#, making my 2D game atm and my problem is how can I give my player a Knock Back when it touches the enemy. How to add it with this code?

public int health;

private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
health -= 20;
//Check for death
if (health <= 0)
{
gameObject.SetActive(false);
}
}
}

• How do you move your player currently? – DMGregory Nov 2 '19 at 4:19
• It's a 2D platformer. It's like Super Mario. :D – Saidus Nov 2 '19 at 8:33
• What I'm looking for here is the code that moves your player. There are many ways to code a 2D platformer, and integrating knockback into each will look a little different. – DMGregory Nov 2 '19 at 11:49
• Here is the code script of the movement of the player: docs.google.com/document/d/e/… – Saidus Nov 4 '19 at 14:57
• If it's important to understand your question, then add the code to your question using the "Edit" link. Assume that any document hosted on another site will not be read. – DMGregory Nov 4 '19 at 16:21

 public float force = 5;
public ForceMode forceMode = ForceMode.Impulse;

private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
Debug.Log("Collision");
health -= 20;
//Check for death
if (health <= 0)
{
gameObject.SetActive(false);
}
else
{
// Calculate Angle Between the collision point and the player
ContactPoint2D contactPoint = collision.GetContact(0);
Vector2 playerPosition = transform.position;
Vector2 dir = contactPoint.point - playerPosition;

// We then get the opposite (-Vector3) and normalize it
dir = -dir.normalized;

GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
GetComponent<Rigidbody2D>().inertia = 0;

//---------------------------------------------------------------------------------------------------------//
//If the problme is not resolved then lock controle key. "playerControles" is a public static boolean which you have declare in the player controller script with true. then in this script you have to enable it or disable it.like
playerControlerScript.playerControles = false; //if its true player input buttons will work and vice versa.
Invoke("EnablePlayerControles", 0.5f); //if then amount of time is long then reduce it to the value you want.

//Also in your player controller script wrap the inputs inside if condition like this if(playerControles){//input/inputs code}.
//---------------------------------------------------------------------------------------------------------//

// And finally we add force in the direction of dir and multiply it by force.
// This will push back the player
}
}
}

private void EnablePlayerControles()
{
playerControlerScript.playerControles = true;
}


Play with force amount and force mode to achieve the effect you want.

I hope this will help.

• for some reason, I am having a problem with that code you gave to me. Like instead of my player getting knockback. it just jumps upwards and sometimes it goes higher. Here is the clip of the problem: im.ezgif.com/tmp/ezgif-1-fe93e23f71a1.gif – Saidus Nov 4 '19 at 15:12
• Sorry i am late. but i thinks its due the current velocity. so when player collide with enemy just make player velocity and force to 0 and then apply the knock back force. also i will suggest to lock your control keys so that you can't apply any extra force and just enable the control keys back like after 0.5f sec may be. – Swati Nov 5 '19 at 3:03
• Also i have updated the code. so test it out. I hope it will work. – Swati Nov 5 '19 at 3:16
• I am having a problem with the "playerControles" is that a variable name of a bool? if so; I am still having the same problem. D: – Saidus Nov 5 '19 at 8:02
• yes it is bool but for now forget about it. and remove those lines. and then test the code. – Swati Nov 5 '19 at 8:04