Unity 2D 碰撞器运行不正常。我无法移动 through/on 二维对撞机下方的一个方块
Unity 2D collider is not behaving properly. I cant move through/on the one block below the 2D collider
所以我试图做一个瓷砖基础运动,突然遇到了 collider2D 的这个问题。
所以我为图像中的那个极点设置了一个 tile 大小的 2D collider,但是有一个问题。
我突然发现我无法通过 2D Collider 下方的 tile/block。
请注意,角色没有任何 collider 2d 或 rigidbody 2d。
该角色依靠函数 private bool notBlocked(Vector3 movePosition)
阻止我的角色移动并在被阻挡时停止动画。
我突出显示了我无法移动的 block/tile。那里没有对撞机,所以我不知道为什么我的角色不能走路 through/on
但是当我在对撞机顶部的方块上尝试时,它与对撞机左右 tile/block 周围的效果相同。
所以这是我用于碰撞时移动和停止移动的代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private float moveSpeed;
public LayerMask SolidObjectLayer;
private bool isMoving;
private Vector2 input;
private Animator animator;
private void Update()
{
InputMove();
}
private void Awake()
{
animator = GetComponent<Animator>();
}
private void InputMove()
{
if (!isMoving)
{
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
if (input.x != 0) input.y = 0;
if (input != Vector2.zero)
{
animator.SetFloat("Horizontal", input.x);
animator.SetFloat("Vertical", input.y);
var movePos = transform.position;
movePos.x += input.x;
movePos.y += input.y;
if (notBlocked(movePos))
StartCoroutine(Move(movePos));
}
}
if (Input.GetKey(KeyCode.LeftShift))
{
moveSpeed = 6f;
animator.speed = 1.5f;
}
else
{
moveSpeed = 4f;
animator.speed = 1f;
}
animator.SetBool("isMoving", isMoving);
}
IEnumerator Move(Vector3 movePos)
{
isMoving = true;
while((movePos - transform.position).sqrMagnitude > Mathf.Epsilon)
{
transform.position = Vector3.MoveTowards(transform.position, movePos, moveSpeed * Time.deltaTime);
yield return null;
}
transform.position = movePos;
isMoving = false;
}
private bool notBlocked(Vector3 movePosition)
{
if(Physics2D.OverlapCircle(movePosition, 0.2f, SolidObjectLayer) != null)
{
return false;
}
return true;
}
}
不知道是不是和“private bool isBlocked()”有关,不过是我用来阻止角色在被阻挡时继续移动的函数
看来你角色的对撞机太大了。如果对撞机覆盖了整个角色,那么头部就会与柱子发生碰撞。要解决此问题,您可以单击此按钮:
并编辑对撞机,使其仅位于脚所在的底部正方形。这将防止头部与柱子碰撞。
好的,所以我设法弄清楚了如何修复它。
所以角色只能在没有碰撞体的方块上移动
所以这意味着如果那个黄色点直接通过对撞机那么它将阻止我的角色移动。所以看起来 OverlapCircle 从角色的中心或枢轴投射,所以我将角色的枢轴稍微降低了一点。
正如你在这里看到的,问题是 OverLapCircle 正试图在对撞机中行走
但随后脚本阻止了它的发生,并阻止我继续在瓷砖下方的时间移动。
所以我试图做一个瓷砖基础运动,突然遇到了 collider2D 的这个问题。 所以我为图像中的那个极点设置了一个 tile 大小的 2D collider,但是有一个问题。 我突然发现我无法通过 2D Collider 下方的 tile/block。
请注意,角色没有任何 collider 2d 或 rigidbody 2d。
该角色依靠函数 private bool notBlocked(Vector3 movePosition)
阻止我的角色移动并在被阻挡时停止动画。
我突出显示了我无法移动的 block/tile。那里没有对撞机,所以我不知道为什么我的角色不能走路 through/on
但是当我在对撞机顶部的方块上尝试时,它与对撞机左右 tile/block 周围的效果相同。
所以这是我用于碰撞时移动和停止移动的代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private float moveSpeed;
public LayerMask SolidObjectLayer;
private bool isMoving;
private Vector2 input;
private Animator animator;
private void Update()
{
InputMove();
}
private void Awake()
{
animator = GetComponent<Animator>();
}
private void InputMove()
{
if (!isMoving)
{
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
if (input.x != 0) input.y = 0;
if (input != Vector2.zero)
{
animator.SetFloat("Horizontal", input.x);
animator.SetFloat("Vertical", input.y);
var movePos = transform.position;
movePos.x += input.x;
movePos.y += input.y;
if (notBlocked(movePos))
StartCoroutine(Move(movePos));
}
}
if (Input.GetKey(KeyCode.LeftShift))
{
moveSpeed = 6f;
animator.speed = 1.5f;
}
else
{
moveSpeed = 4f;
animator.speed = 1f;
}
animator.SetBool("isMoving", isMoving);
}
IEnumerator Move(Vector3 movePos)
{
isMoving = true;
while((movePos - transform.position).sqrMagnitude > Mathf.Epsilon)
{
transform.position = Vector3.MoveTowards(transform.position, movePos, moveSpeed * Time.deltaTime);
yield return null;
}
transform.position = movePos;
isMoving = false;
}
private bool notBlocked(Vector3 movePosition)
{
if(Physics2D.OverlapCircle(movePosition, 0.2f, SolidObjectLayer) != null)
{
return false;
}
return true;
}
}
不知道是不是和“private bool isBlocked()”有关,不过是我用来阻止角色在被阻挡时继续移动的函数
看来你角色的对撞机太大了。如果对撞机覆盖了整个角色,那么头部就会与柱子发生碰撞。要解决此问题,您可以单击此按钮:
好的,所以我设法弄清楚了如何修复它。
所以角色只能在没有碰撞体的方块上移动
所以这意味着如果那个黄色点直接通过对撞机那么它将阻止我的角色移动。所以看起来 OverlapCircle 从角色的中心或枢轴投射,所以我将角色的枢轴稍微降低了一点。
正如你在这里看到的,问题是 OverLapCircle 正试图在对撞机中行走
但随后脚本阻止了它的发生,并阻止我继续在瓷砖下方的时间移动。