简体   繁体   中英

Unity: Knockback my player

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 image will show you the different direction of which my player may get knock back. My problem: X=0,Y=1 (up) and X=0,Y=-1 (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:

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

Most probably, it doesn't see the if statements of y part. Use else if for all if statements after the first one. Try this:

 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))
                && initialHitPoint.y > (this.transform.position.y + (this.transform.localScale.y / 3)))
        {
            xForce = 1;
            yForce = 1;
        }
        else if (initialHitPoint.x > (this.transform.position.x + (this.transform.localScale.x / 3))
                && initialHitPoint.y < (this.transform.position.y - (this.transform.localScale.y / 3)))
        {
            xForce = 1;
            yForce = -1;
        }
        else if (initialHitPoint.x < (this.transform.position.x + (this.transform.localScale.x / 3))
                && initialHitPoint.y > (this.transform.position.y - (this.transform.localScale.y / 3)))
        {
            xForce = -1;
            yForce = 1;
        }
        else if (initialHitPoint.x < (this.transform.position.x + (this.transform.localScale.x / 3))
                && initialHitPoint.y < (this.transform.position.y - (this.transform.localScale.y / 3)))
        {
            xForce = -1;
            yForce = -1;
        }
        else if (initialHitPoint.x > (this.transform.position.x + (this.transform.localScale.x / 3))
                && initialHitPoint.y == (this.transform.position.y - (this.transform.localScale.y / 3)))
        {
            xForce = 1;
            yForce = 0;
        }
        else if (initialHitPoint.x < (this.transform.position.x + (this.transform.localScale.x / 3))
                && initialHitPoint.y == (this.transform.position.y - (this.transform.localScale.y / 3)))
        {
            xForce = -1;
            yForce = 0;
        }
        else if (initialHitPoint.x == (this.transform.position.x + (this.transform.localScale.x / 3))
                && initialHitPoint.y < (this.transform.position.y - (this.transform.localScale.y / 3)))
        {
            xForce = 0;
            yForce = -1;
        }
        else if (initialHitPoint.x == (this.transform.position.x + (this.transform.localScale.x / 3))
                && initialHitPoint.y > (this.transform.position.y - (this.transform.localScale.y / 3)))
        {
            xForce = 0;
            yForce = 1;
        }
        else
        {
            xForce = 0;
            yForce = 0;
        }
        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