Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Help with Knockback

Discussion in '2D' started by Flauros1, Jul 7, 2020.

  1. Flauros1

    Flauros1

    Joined:
    Jul 2, 2020
    Posts:
    22
    Ey guys first of all sorry for my bad english and thanks you sou much for read me.

    I have troubles with knockback, with this code my player only Jump when collider with an enemy

    public void EnemyKnockBack(float enemyPosX)
    {
    jump = true;

    float side = Mathf.Sign(enemyPosX - transform.position.x);
    rb.AddForce(Vector2.left * side * fuerzaSalto, ForceMode2D.Impulse);
    }


    What i'm doing wrong? My player only jump when the enemy touchs him but I need the knockback!!
    Thanks you so much
     
  2. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    883
    Hello @Flauros1,
    So what more exactly happens when you run this code? By the looks of it, it should add an instant force to the GameObject with the Rigidbody2D attached to it, in either positive or negative x axis.
     
  3. Flauros1

    Flauros1

    Joined:
    Jul 2, 2020
    Posts:
    22
    The enemy only jump after this :(

    (The enemy jump is on Y, he don't make the Knockback)
     
  4. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    883
    What I usually do when faced with a coding mystery like this, is to break the issue down a bit.
    What happens if you create a new GameObject, add a rigidbody2D onto that GameObject, and a script with only that knock back code, being executed on key down or something similar? Does it behave correctly?
    Then you can either scale it even further back, or if it works, start adding back the pieces to see when the issue starts to appear again.
     
  5. Mooxe

    Mooxe

    Joined:
    Sep 18, 2013
    Posts:
    19
    Try multiplying a big number and see if it makes any changes.
    Code (CSharp):
    1. rb.AddForce(Vector2.left * side * fuerzaSalto * 3000, ForceMode2D.Impulse);
     
  6. Flauros1

    Flauros1

    Joined:
    Jul 2, 2020
    Posts:
    22
    Still not working uhm....
     
  7. Dedi6

    Dedi6

    Joined:
    Jan 20, 2019
    Posts:
    119
    I'm guessing you have a function which moves the character in your fixedUpdate or Update (even tho it shouldn't be in update) which zeros out the velocity on the x axis.
    Just toggle it off when you are knocked back.
    You can do it with a bool, state machine or whatever you want.
     
  8. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    883
    @Flauros1, did you try to break the functionality down a bit and test each piece of it? That tends to highlight what causes the issue.
     
  9. Flauros1

    Flauros1

    Joined:
    Jul 2, 2020
    Posts:
    22
    Well I tried this for the Knockback.. but only works with Vector2.up
    It doesn't works with vector2.left why? xD
     

    Attached Files:

  10. Dedi6

    Dedi6

    Joined:
    Jan 20, 2019
    Posts:
    119