简体   繁体   English

Unity:沿Y轴击回我的播放器

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

I don't think the Y-axis for my code is working. 我认为代码的Y轴无法正常工作。 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. 我尝试增加yForceToAdd和localScale.y,但是(当我与附加了此脚本的对象发生冲突时)我的播放器只会(当我在对象顶部发生冲突时)X = 1,Y = 1或X = -1,Y = 1,而不是X = 0,Y = 1。 the same problem with the bottom of my object X=0, Y=-1 doesn't seem to work either. 我的对象底部X = 0,Y = -1的问题也似乎不起作用。 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..): 例如在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