我将此脚本附加到一个对象上,如果我的玩家遇到该对象,它将被击退。我有六个方向可以被击退。这个image将向您显示我的玩家可能会被击退的不同方向。我的问题: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);
}
}
}
答案 0 :(得分:0)
最有可能的是,它没有看到y部分的if
语句。在第一个语句之后的所有else if
语句中使用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);
}
}