团结:击退我的球员
本文关键字:我的 团结 | 更新日期: 2023-09-27 18:27:15
我把这个脚本附加到一个对象上,如果我的玩家遇到那个对象,它就会被击退。我有六个方向,它将被击退。这张图片将向你展示我的球员可能会被击倒的不同方向。我的问题是:X=0,Y=1
(上行)和X=0,Y=-1
(下行)不起作用,但其他速度都起作用。我如何还包括X=0,Y=1和X=0,Y=-1的方向。谢谢,这是我的代码:
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);
}
}
}
很可能,它没有看到y部分的if
语句。对第一个语句之后的所有if
语句使用else if
。试试这个:
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);
}
}