Unity Physics2D.Raycast流行起来 [英] Unity Physics2D.Raycast hits itself

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

问题描述

我正在尝试使用Physics2D.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始终报告"Player"本身,我不知道这是怎么可能的.那怎么了?

...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 ,然后确保查询从碰撞器开始未检查.更改该代码后,您的代码可以正常运行.这是屏幕截图:

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 .另一种解决方案是使用图层.进行射线广播,但忽略Player图层.在执行此操作之前,请确保创建一个名为Ground的图层,并将地面GameObject放置在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.

对于其他人来说,下面是其他一些方法,可以轻松检测Player何时触地,而无需使用Physics2D.Raycast或执行上面的所有操作.

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天全站免登陆