简体   繁体   中英

Unity: Knockback my player along the Y-axis

I don't think the Y-axis for my code is working. Iv'e tried increasing the yForceToAdd and the localScale.y but (when I collide with the object that has this script attached to it) my player only goes (when I collide at the top of the object) X=1, Y=1 or X=-1, Y=1 and not X=0, Y=1 as well. the same problem with the bottom of my object X=0, Y=-1 doesn't seem to work either. can someone help with this problem?

public float xForceToAdd;
public float yForceToAdd;

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 < Rigidbody2D > ();

        //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;
        }

        rigidForForce.velocity = new Vector2(xForce * xForceToAdd, yForce * yForceToAdd);
    }
}

The logic in computing the margins where the hit is being registered is wrong. For example in this.transform.localScale.y / 3 , the localScale.y with probably give you 1.0f , but what you meant inside the expression (this.transform.position.y + (this.transform.localScale.y / 3) is that you want the y-center of the object, plus one-third of it's height. However, this.transform.localScale.y will only give you a "multiplier" so to say. In a unscaled object, transfomr.localScale will be 1.0 , so you would be adding transform.position.y + (1.0f / 3) , which is probably not what you want. You must multipliy this with the actual height of the object to get what you want. This can be done by either relying on the Sprite or a Collider , eg a BoxCollider2D . Modified logic (I also divided by 3f instead of by 3 to make it a more accurate floating point division..):

public float xForceToAdd;
public float yForceToAdd;

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 < Rigidbody2D > ();

        //Get the width and height of this object by looking up the size of the box collider
        //Alternatively, use constant values here or rely on the Sprite.
        float width = GetComponent<BoxCollider2D>().size.x;
        float height = GetComponent<BoxCollider2D>().size.y;

        //Determine left right center of X hit
        if (initialHitPoint.x > (this.transform.position.x + width * (this.transform.localScale.x / 3f)))
            xForce = 1;
        else if (initialHitPoint.x < (this.transform.position.x -  width* (this.transform.localScale.x / 3f)))
            xForce = -1;
        else
            xForce = 0;

        if (initialHitPoint.y > (this.transform.position.y + height * (this.transform.localScale.y / 3f)))
            yForce = 1;
        else if (initialHitPoint.y < (this.transform.position.y - height * (this.transform.localScale.y / 3f)))
            yForce = -1;
        else
            yForce = 0;

        Debug.Log(string.Format("Hit Point X: {0}. Left Boundary: {1} Right Boundary: {2}, xForce = {3}", initialHitPoint.x, (this.transform.position.x  - width*this.transform.localScale.x / 3f), (this.transform.position.x + width * (this.transform.localScale.x / 3f)), xForce));


        rigidForForce.velocity = new Vector2(xForce * xForceToAdd, yForce * yForceToAdd);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM