Physics2D.Raycast 适用于某些对象但不适用于其他对象

Physics2D.Raycast Works On Some Objects But Doesn't Work On Others

所以我正在尝试制作一个超级马里奥兄弟的克隆来练习。我正在创造世界 1-1。一切都运行良好:我可以移动,敌人可以移动并被杀死,方块可以像预期的那样进行交互。但此时我正在使用对撞机,它们时不时会造成一些问题,而且当敌人面对对撞机时,我也找不到让敌人朝相反方向移动的方法。所以我看了看其他人,发现他们正在使用光线投射。然后决定自己试试。敌人脚本没有问题

敌人脚本

private void RayCast()
{
    // Getting the max distance by using the size of the collider and creating the ray itself
    float rayMaxDistance = GetComponent<BoxCollider2D>().bounds.size.x/2;
    Ray2D ray = new Ray2D(transform.position, Vector2.right);
    
    // This way ray will be casted in the direction the enemy goes.
    if (rb.velocity.x > 0)
    {
        RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, rayMaxDistance);

        if (hit.collider != null)
        {
            if (hit.collider.gameObject.tag != "Player")
            {
                // Moving in the opposite direction
                rb.velocity = Vector2.zero;
                rb.AddForce(-movementVector, ForceMode2D.Impulse);
            }
        }
        //Debug.DrawRay(ray.origin, ray.direction, Color.red);
        
    }
    // This way ray will be casted in the direction the enemy goes.
    else if (rb.velocity.x < 0)
    {
        ray.direction = Vector3.left;
        RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, rayMaxDistance);

        if (hit.collider != null)
        {
            if (hit.collider.gameObject.tag != "Player")
            {
                // Moving in the opposite direction
                rb.velocity = Vector2.zero;
                rb.AddForce(movementVector, ForceMode2D.Impulse);
            }
        }
        //Debug.DrawRay(ray.origin, ray.direction, Color.red);

它按预期工作,但是当我尝试在 Mario 上使用它时,它不知何故不起作用。

角色脚本中的部分。 (还没有完成,因为现在连功能都用不了了)

private void ActivateLuckyBlock()
{
    float maxRayDistance = GetComponent<BoxCollider2D>().bounds.size.y/2;
    Ray2D ray = new Ray2D(transform.position, Vector2.up);

    RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, maxRayDistance);

    if (hit.collider != null)
    {
        Debug.Log("There is something");  
    }
}

我一直在互联网上寻找任何类型的答案,但找不到任何相关信息。我不知道我做错了什么。它甚至不是一个完整的实现,只是想测试一下。尽管如此,它还是行不通。请在我发疯之前帮助我。

射线原点应该是边界的中心并尝试为maxRayDistance添加一点额外的space,(bounds.extents等于边界.size/2:documentation for bounds.extent)

float maxRayDistance = GetComponent<BoxCollider2D>().bounds.extents.y + 0.001f;
Ray2D ray = new Ray2D(GetComponent<BoxCollider2D>().bounds.center, Vector2.up);

尝试更改 Debug.Log,这将使您知道光线投射击中的对象的名称:

Debug.Log("Raycast hit:" + hit.collider.name);

如果您仍然遇到问题,请创建一个 public layerMask 变量,为您要在以下位置进行光线投射的对象创建并添加一个图层:

RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, maxRayDistance, layerMask);

通过更新或固定更新调用函数:

它应该看起来像这样:

public LayerMask luckyBlockMask;
    void Update()
    {
        ActivateLuckyBlock();
    }
    private void ActivateLuckyBlock()
    {
        float maxRayDistance = GetComponent<BoxCollider2D>().bounds.extents.y + 0.1f;
        Ray2D ray = new Ray2D(GetComponent<BoxCollider2D>().bounds.center, Vector2.up);

        RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, maxRayDistance,
        luckyBlockMask);

        if (hit.collider != null)
        {
            Debug.Log("Raycast hit:" + hit.collider.name);
        }
    }