Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Resolved Simple knockback only works in one direction.

Discussion in '2D' started by Geodesic_Void, May 22, 2024.

  1. Geodesic_Void

    Geodesic_Void

    Joined:
    Apr 4, 2024
    Posts:
    26
    Hey.! just trying to write a simple knock back to the enemy after being hit.
    Works perfect in the one direction but the other direction he gets knocked toward me and I cant seem to pinpoint why. Anyways, thanks for taking a look if you do.!
    (If you need anymore code, or any more information just let me know)
    Code (CSharp):
    1.   public void TakeDamage(int attackDamage)
    2.   {
    3.       Debug.Log("Took Damage");
    4.       currentHealth -=attackDamage;
    5.       ani.SetTrigger("Hit");
    6.      
    7.       if (look.isRight == false)
    8.       {
    9.           rb.AddForce(Vector2.left * launchBackSpeed * 3, ForceMode2D.Impulse);
    10.           rb.AddForce(Vector2.up * launchBackSpeed * 1.5f, ForceMode2D.Impulse);
    11.  
    12.       }
    13.       if (look.isRight == true)
    14.       {
    15.           rb.AddForce(Vector2.right * launchBackSpeed * 3, ForceMode2D.Impulse);
    16.           rb.AddForce(Vector2.up * launchBackSpeed * 1.5f, ForceMode2D.Impulse);
    17.       }
    18.  
    19.  
    20.       if (currentHealth <= 0)
    21.       {
    22.           ani.SetBool("Dead",true);
    23.           Invoke("Die", 1f);
    24.       }
    25.  
    26.   }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    Your post is marked as Resolved... do you still have a problem? If so, it is time to start debugging!

    By debugging you can find out exactly what your program is doing so you can fix it.

    https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

    Use the above techniques to get the information you need in order to reason about what the problem is.

    You can also use
    Debug.Log(...);
    statements to find out if any of your code is even running. Don't assume it is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.