1
\$\begingroup\$

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);
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ How do you move your player currently? \$\endgroup\$ – DMGregory Nov 2 '19 at 4:19
  • \$\begingroup\$ It's a 2D platformer. It's like Super Mario. :D \$\endgroup\$ – Saidus Nov 2 '19 at 8:33
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Nov 2 '19 at 11:49
  • \$\begingroup\$ Here is the code script of the movement of the player: docs.google.com/document/d/e/… \$\endgroup\$ – Saidus Nov 4 '19 at 14:57
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Nov 4 '19 at 16:21
0
\$\begingroup\$

I share some code which will help you out:

 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
            GetComponent<Rigidbody2D>().AddForce(dir * force, forceMode);
        }
    }
}

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

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

I hope this will help.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – Saidus Nov 4 '19 at 15:12
  • \$\begingroup\$ 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. \$\endgroup\$ – Swati Nov 5 '19 at 3:03
  • \$\begingroup\$ Also i have updated the code. so test it out. I hope it will work. \$\endgroup\$ – Swati Nov 5 '19 at 3:16
  • \$\begingroup\$ 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: \$\endgroup\$ – Saidus Nov 5 '19 at 8:02
  • \$\begingroup\$ yes it is bool but for now forget about it. and remove those lines. and then test the code. \$\endgroup\$ – Swati Nov 5 '19 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.