我正在尝试制作口袋妖怪游戏,但碰撞检测有问题。例如,如果我想在这里右转:
我应该可以做到,但我不能动。两个对象都有 BoxCollider2D。这是玩家的 BoxCollider:
树有一个大小为 1, 1 和偏移量为 0, 0 的 boxcollider。
这是我的代码:
void Update()
{
input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
actualSpeed = Input.GetKey(KeyCode.LeftShift) ? walkingVelocity : runingVelocity;
if (input != Vector2.zero && p == transform.position)
{
anim.SetBool("isMoving", true);
if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
{
if (input.x > 0)
{
if (direction.Equals(Direction.Este) && canMove())
{
p += Vector3.right;
}else
{
direction = Direction.Este;
}
}
else
{
if (direction.Equals(Direction.Oeste) && canMove()) {
p -= Vector3.right;
}
else{
direction = Direction.Oeste;
}
}
}
else
{
if (input.y > 0)
{
if (direction.Equals(Direction.Norte) && canMove())
{
p += Vector3.up;
}else
{
direction = Direction.Norte;
}
}
else
{
if (direction.Equals(Direction.Sur) && canMove())
{
p -= Vector3.up;
}
else
{
direction = Direction.Sur;
}
}
}
anim.SetFloat("input_x", input.x);
anim.SetFloat("input_y", input.y);
}
else if (input == Vector2.zero)
{
anim.SetBool("isMoving", false);
}
transform.position = Vector3.MoveTowards(transform.position, p, actualSpeed * Time.deltaTime);
}
bool canMove()
{
bool b = true;
Ray2D r;
if (direction.Equals(Direction.Norte))
{
r = new Ray2D(transform.position, Vector3.up);
}
else if (direction.Equals(Direction.Sur))
{
r = new Ray2D(transform.position, Vector3.down);
}
else if (direction.Equals(Direction.Este))
{
r = new Ray2D(transform.position, Vector3.right);
}
else
{
r = new Ray2D(transform.position, Vector3.left);
}
Debug.DrawRay(r.origin, r.direction);
RaycastHit2D hit = Physics2D.Raycast(r.origin, r.direction, 1f , 1 << 8);
if (hit.collider != null)
{
if (hit.collider.CompareTag("Obstacle"))
{
b = false;
}
}
return b;
}
我怎样才能让玩家在树上移动?
谢谢!PD。我对 Unity2D 很陌生