Unity Physics2D.Raycast 击中自身 [英] Unity Physics2D.Raycast hits itself

查看:128
本文介绍了Unity Physics2D.Raycast 击中自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Physics2D.Raycast 来检查玩家是否在地面上(我知道还有其他方法可以检查玩家是否在地面上,但我认为raycast 是最可靠的).问题是,在我的场景中,它将玩家本身作为命中返回,我真的不明白为什么以及我应该做什么.我的代码(在 PlayerController 中)如下:

I'm trying to use Physics2D.Raycast in order to check if the player is on the ground (I know that there are other approaches to check if the player is on the ground but I think that the raycast is the most reliable). The problem is that in my scenario it returns the player itself as hit and I really don't understand why and what should I do. My code (in PlayerController) is the following:

 public bool IsGrounded () {
        Bounds bounds = this.playerCollider.bounds;
        Vector3 rayOrigin = bounds.center;
        rayOrigin.y -= bounds.extents.y;
        RaycastHit2D hit = Physics2D.Raycast (rayOrigin, Vector2.down, 0.1f);
        if (hit.collider != null) {
            Debug.Log ("Collider is: " + hit.collider.name);
        }
        return hit.collider != null;
    }

我可以使用以下方法调试投射的光线:

And I can debug the casted ray using:

Debug.DrawLine (rayOrigin, new Vector3 (rayOrigin.x, rayOrigin.y - 0.1f, rayOrigin.z), Color.magenta);

...并且它按预期进行转换,而 Debug.Log 总是报告播放器"本身,我不知道这怎么可能.怎么了?

...and it gets casted as expected, while the Debug.Log always reports "Player" which is itself and I don't know how it's possible. So what's wrong?

ps.我使用的是 Unity 5.3

ps. I'm using Unity 5.3

推荐答案

出现问题是因为您的播放器在光线投射开始时重叠.有几种方法可以解决此问题:

The problem is occurring because your Player is overlapping at the start of the raycast. There are few ways to fix this:

1.禁用查询开始于碰撞器.

转到编辑->项目设置->Physics 2D,然后确保Queries Start In Colliders未检查.更改后,您的代码运行良好.这是截图:

Go to Edit->Project Settings->Physics 2D then make sure that Queries Start In Colliders is NOT checked. Your code ran fine after changing that. Here is a screenshot:

2.另一种解决方案是使用layers.Raycasting,但忽略Player 层.在此之前,请确保创建一个名为Ground 并将您的地面游戏对象放在 Ground 层,然后创建另一个名为 Player 的层,并将您的播放器放在 Player 层.我们现在可以使用 bitwise 运算符从光线投射中排除 Player 层.

2.Another solution is to use layers.Raycasting but ignoring the Player layer.Before you do that, make sure to create a layer called Ground and put your ground GameObject to the Ground layer then create another layer called Player and put your player in the Player layer. We can now use bitwise operator to exclude Player layer from the raycast.

现在,假设播放器层编号为 9.下面的代码应该可以解决您的问题.

Now, lets assume that Player layer number is 9. The code below should fix your problem.

public int playerLayer = 9;
int layerMask = ~(1 << playerLayer); //Exclude layer 9
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.down, 0.1f, layerMask);

就是这样.这两种方法都能够解决您的问题.

That's it. Both of these were able to solve your problem.

对于阅读的其他人,以下是无需使用 Physics2D.Raycast 或执行上述所有操作即可轻松检测 Player 何时接触地板的其他方法.

For other people reading, below are other ways to easily detect when Player is touching the floor without using Physics2D.Raycast or doing all those things above.

只需附加到 Player.

public class Player : MonoBehaviour
{
    public LayerMask groundLayer;
    Collider2D playerCollider;
    bool grounded;

    void Start()
    {
        playerCollider = gameObject.GetComponent<Collider2D>();
    }

    public bool IsGrounded()
    {
        grounded = Physics2D.OverlapCircle(playerCollider.transform.position, 1, groundLayer);
        return grounded;
    }
}

或者您可以使用 IsTouchingLayers.

public bool IsGrounded()
{
    grounded = grounded = playerCollider.IsTouchingLayers(groundLayer.value);
    return grounded;
}

这篇关于Unity Physics2D.Raycast 击中自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆