简体   繁体   English

团结:全方位击退

[英]Unity: knockback in all directions

I want to make my player bounce in all directions when it collides with my object, I know that when it hit the left side of my object my player gets knock back to the left and the same with the right side, but how can I include the top and bottom of my object. 我想让播放器与我的物体碰撞时会向各个方向弹跳,我知道当它撞击到我物体的左侧时,播放器会向左敲回,而右侧也一样,但是我该如何包括我对象的顶部和底部。 If I'm not making any sense please look at these pictures (X=0 Y=1) and (X=0 Y=-1). 如果我没有任何意义,请查看这些图片(X = 0 Y = 1)和(X = 0 Y = -1)。 ( http://noobtuts.com/content/unity/2d-pong-game/vector2_directions.png ) http://noobtuts.com/content/unity/2d-pong-game/vector2_directions.png

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

}

Do this instead 改为这样做

void OnTriggerEnter2D(Collider2D other)  {
    if (other.gameObject.tag == "Player") {
        Vector2 v = other.gameObject.transform.position - this.transform.position;
        //Grab our collided with objects rigibody
        Rigidbody2D rigidForForce = other.gameObject.GetComponent<Rigidbody2D>();
        rigidForForce.velocity = v.normalized;
    }
}

Rather than figuring out if you should go up/down/left/right, use the vector math functions. 不必弄清楚是否应该向上/向下/向左/向右,而是使用矢量数学函数。 If you're only interested in pushing in the orthogonal directions, do this for the last line 如果您只想在正交方向上推动,请对最后一行进行操作

        boolean onX = Mathf.Abs(v.x) > Mathf.Abs(v.y);
        rigidForForce.velocity = new Vector2(Mathf.Sign(v.x)*onX?1:0,Mathf.Sign(v.y)*onX?0:1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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