Unity:沿着Y轴击退我的玩家

本文关键字:我的 玩家 沿着 Unity | 更新日期: 2023-09-27 18:27:25

我认为我的代码的Y轴不工作。Iv'e尝试增加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);
    }
}

Unity:沿着Y轴击退我的玩家

计算注册命中的边距的逻辑是错误的。例如,在this.transform.localScale.y / 3中,带有的localScale.y可能会给你1.0f,但你在表达式(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);
    }
}