title: Unity: Knockback my player
body: <p>I have this script attached to an object, if my player encounters that object it will be knocked back. I have six directions on which it will be knocked back. This <a href="http://noobtuts.com/content/unity/2d-pong-game/vector2_directions.png" rel="nofollow">image</a> will show you the different direction of which my player may get knock back. My problem: <code>X=0,Y=1</code> (up) and <code>X=0,Y=-1</code> (down) doesn't work, however every other velocity works. How can I also include the directions of X=0,Y=1 and X=0,Y=-1. Thank you and here is my code:</p>
  
  <pre><code>public class Knockback: MonoBehaviour
  {
      public float xForceToAdd;
      public float yForceToAdd;
  
      // Use this for initialization
      void Start()
      {
  
      }
  
      // Update is called once per frame
      void Update()
      {
  
      }
  
      void OnTriggerEnter2D(Collider2D other)
      {
          if (other.gameObject.tag == "Player")
          {
              //Store the vector 2 of the location where the initial hit happened;
              Vector2 initialHitPoint = new Vector2(other.gameObject.transform.position.x, other.gameObject.transform.position.y);
              float xForce = 0;
              float yForce = 0;
              //Grab our collided with objects rigibody
              Rigidbody2D rigidForForce = other.gameObject.GetComponent &lt; Rigidbody2D &gt; ();
              //Determine left right center of X hit
              if (initialHitPoint.x &gt; (this.transform.position.x + (this.transform.localScale.x / 3)))
              {
                  xForce = 1;
              }
              else if (initialHitPoint.x &lt; (this.transform.position.x - (this.transform.localScale.x / 3)))
              {
                  xForce = -1;
              }
              else
              {
                  xForce = 0;
              }
              if (initialHitPoint.y &gt; (this.transform.position.y + (this.transform.localScale.y / 3)))
              {
                  yForce = 1;
              }
              else if (initialHitPoint.y &lt; (this.transform.position.y - (this.transform.localScale.y / 3)))
              {
                  yForce = -1;
              }
              else
              {
                  yForce = 0;
              }
              rigidForForce.velocity = new Vector2(xForce * xForceToAdd, yForce * yForceToAdd);
          }
      }
  }
  </code></pre>