我有一个要在水平线上移动的块,因此我想使其移动到某个角(例如:右上边界),使其不会在任何位置移动。常规的碰撞检测不起作用,因为我正在短距离跳跃(player.transform.position.x-1等)移动块。
这是我正在使用的代码:
public void OnCollisionEnter2D(Collision2D collision)
{ if (collision.gameObject.tag == "barrier" && (GetComponent<BoxCollider2D>().tag == "top" && GetComponent<BoxCollider2D>().tag == "left"))
{
player.canMoveUp = false;
player.canMoveDown = true;
player.canMoveLeft = false;
player.canMoveRight = true;
}
else if (collision.gameObject.tag == "barrier" && (GetComponent<BoxCollider2D>().tag == "top" && GetComponent<BoxCollider2D>().tag == "right"))
{
player.canMoveUp = false;
player.canMoveDown = true;
player.canMoveLeft = true;
player.canMoveRight = false;
}
else if (collision.gameObject.tag == "barrier" && (GetComponent<BoxCollider2D>().tag == "bottom" && GetComponent<BoxCollider2D>().tag == "left"))
{
player.canMoveUp = true;
player.canMoveDown = false;
player.canMoveLeft = false;
player.canMoveRight = true;
}
else if (collision.gameObject.tag == "barrier" && (GetComponent<BoxCollider2D>().tag == "bottom" && GetComponent<BoxCollider2D>().tag == "right"))
{
player.canMoveUp = true;
player.canMoveDown = false;
player.canMoveLeft = true;
player.canMoveRight = false;
}
}
public void OnCollisionExit2D(Collision2D collision)
{
Debug.Log("notTouching");
player.canMoveUp = true;
player.canMoveDown = true;
player.canMoveLeft = true;
player.canMoveRight = true;
}
编辑:新代码。
if (Input.GetKeyUp(KeyCode.LeftArrow))
{
Vector2 movementLeft = new Vector2(-1, 0);
RaycastHit2D hitLeft = Physics2D.Raycast(transform.position, movementLeft, -1);
if (hitLeft.collider && hitLeft.collider.gameObject.tag == "barrier")
{
Debug.Log("N/A");
}
else
{
player.transform.position += new Vector3(movementLeft.x,movementLeft.y,0);
}
}
else if (Input.GetKeyUp(KeyCode.RightArrow))
{
Vector2 movementRight = new Vector2(1, 0);
RaycastHit2D hitRight = Physics2D.Raycast(transform.position, movementRight, 1);
if (hitRight.collider && hitRight.collider.gameObject.tag == "barrier")
{
Debug.Log("N/A");
}
else
{
player.transform.position += new Vector3(movementRight.x, movementRight.y, 0);
}
}
else if (Input.GetKeyUp(KeyCode.DownArrow))
{
Vector2 movementDown = new Vector2(0, -1);
RaycastHit2D hitDown = Physics2D.Raycast(transform.position, movementDown, -1);
if (hitDown.collider && hitDown.collider.gameObject.tag == "barrier")
{
Debug.Log("N/A");
}
else
{
player.transform.position += new Vector3(movementDown.x, movementDown.y, 0);
}
}
else if (Input.GetKeyUp(KeyCode.UpArrow))
{
Vector2 movementUp = new Vector2(0, 1);
RaycastHit2D hitUp = Physics2D.Raycast(transform.position, movementUp, 1);
if (hitUp.collider && hitUp.collider.gameObject.tag == "barrier")
{
Debug.Log("N/A");
}
else
{
player.transform.position += new Vector3(movementUp.x, movementUp.y, 0);
}
}
else
{
Debug.Log("N/A");
}
答案 0 :(得分:0)
更简单的解决方案是使用Physics.Raycast2D
在移动之前检查移动过程中是否有任何障碍。
这是代码的粗略构想:
// Some code to get input and end up with a movement vector
...
movement = new Vector2(1, 0);
// Raycast in the direction of movement with a distance of 1.
RaycastHit2D hit = Physics2D.Raycast(transform.position, movement, 1);
// We hit something and its a barrier
if (hit.collider && hit.collider.gameobject.tag == "barrier")
{
// Don't move, play sound that indicate you hit a barrier or some sort
}
else
{
// Move to the new position
transform.position += new Vector3(movement.x, movement.y, 0);
}