Vector 2 direction problem (knockback)

Can someone help with this problem? please look at this image for help (http://noobtuts.com/content/unity/2d-pong-game/vector2_directions.png)

  1. When player collides with object, player should get knock back

  2. Depending on where the player hits I want the script to calculate the player’s direction when it bounces off the object

  3. The script does that, but the Y-axis is not working properly

  4. When the player hits the center of the objects box collider the player is sent of diagonally not straight forward

  5. I want (when my player hit the center on the box collider) my player to go straight forward. This is my code:

     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();
    //Determine left right center of X hit
    if (initialHitPoint.x > (this.transform.position.x + (this.transform.localScale.x /3)))
    {
    xForce = 1;
    }
    else if (initialHitPoint.x < (this.transform.position.x - (this.transform.localScale.x /3)))
    {
    xForce = -1;
    }
    else
    {
    xForce = 0;
    }
    if (initialHitPoint.y > (this.transform.position.y + (this.transform.localScale.y /3)))
    {
    yForce = 1;
    }
    else if (initialHitPoint.y < (this.transform.position.y - (this.transform.localScale.y /3)))
    {
    yForce = -1;
    }
    else
    {
    yForce = 0;
    }
    Debug.Log("xForce: " + xForce + " xForceToAdd: " + xForceToAdd + " yForce: " + yForce + " yForceToAdd: " + yForceToAdd);
    rigidForForce.velocity = new Vector2(xForce * xForceToAdd, yForce * yForceToAdd);
    }

    }

I attached a debug.log to see the co-ordinate I was receiving when I hit the center top of the object and got this: xForce: 1 xForceToAdd: 100 yForce: 1 yForceToAdd: 100

Cleaning up your code a bit so we can see a bit more clearly what is happening and print out a bit more useful information:

             void OnTriggerEnter2D(Collider2D other)  
             {
                 if (other.gameObject.tag == "Player")
                 {
                     //Store the vector 2 of the location where the initial hit happened 
                     //(note: Vector3 automatically converts to Vector2 by removing z);
                     Vector2 initialHitPoint = other.gameObject.transform.position;

                     //calculate the minimum and maximum 'extents' that we will use for picking direction
                     Vector2 extentSize = transform.localScale / 3.0f;
                     Vector2 centre = transform.position;
                     Vector2 minExtent = centre-extentSize;
                     Vector2 maxExtent = centre+extentSize;

                     //Determine x direction to push
                     float xForce = 0;
                     if (initialHitPoint.x > maxExtent.x)
                         xForce = 1;
                     else if (initialHitPoint.x < minExtent.x)
                         xForce = -1;
                     else
                         xForce = 0;

                     //Determine y direction to push
                     float yForce = 0;
                     if (initialHitPoint.y > maxExtent.y)
                         yForce = 1;
                     else if (initialHitPoint.y < minExtent.y)
                         yForce = -1;
                     else
                         yForce = 0;

                     //calculate the velocity to apply
                     Vector2 newVelocity = new Vector2(xForce * xForceToAdd, yForce * yForceToAdd); 

                     //print out lots of stuff
                     Debug.Log("Hit point: " + initialHitPoint.ToString());
                     Debug.Log("Min extent: " + minExtent.ToString());
                     Debug.Log("Max extent: " + maxExtent.ToString());
                     Debug.Log("New velocity: " + newVelocity.ToString());

                     //Grab our collided with objects rigibody and apply velocity
                     Rigidbody2D rigidForForce = other.gameObject.GetComponent<Rigidbody2D>();
                     rigidForForce.velocity = newVelocity;
                 }
             }

It generally looks Ok from a logic point of view. All I can suggest is that you are either mistaken about where the ‘centre’ of your object or the player is, or the localScale is not representative of its actual size in the world.

Those debug prints should tell you exactly what is happening though, as I am now calculating the maximum extent and minimum extent and printing them, so you can read them for yourself and determine whether they are what you think they should be.

(note: just realised that code relies on ‘localScale’ not being negative!

Hope that helps - I appreciate it isn’t a solution, but it hopefully puts you well on the way to finding one.

-Chris