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