簡體   English   中英

Unity:沿Y軸擊回我的播放器

[英]Unity: Knockback my player along the Y-axis

我認為代碼的Y軸無法正常工作。 我嘗試增加yForceToAdd和localScale.y,但是(當我與附加了此腳本的對象發生沖突時)我的播放器只會(當我在對象頂部發生沖突時)X = 1,Y = 1或X = -1,Y = 1,而不是X = 0,Y = 1。 我的對象底部X = 0,Y = -1的問題也似乎不起作用。 有人可以幫助解決這個問題嗎?

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

計算命中記錄的邊距時的邏輯是錯誤的。 例如在this.transform.localScale.y / 3 ,可能帶有1.0flocalScale.y ,但在表達式(this.transform.position.y + (this.transform.localScale.y / 3)是您想要對象的y中心加上其高度的三分之一,但是this.transform.localScale.y只會給您一個“乘數”,也就是說,在未縮放的對象中, transfomr.localScale將為1.0 ,所以您將添加transform.position.y + (1.0f / 3) ,這可能不是您想要的,必須將其乘以對象的實際高度才能獲得所需的值。通過依賴SpriteCollider (例如BoxCollider2D 。修改后的邏輯(我也用3f除以3進行除法,以使其更精確地進行浮點除法..):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM