简体   繁体   English

在 Unity 中使用 Raycast 进行碰撞检测

[英]Collision Detection with Raycast in Unity

The problem is about as simple as it sounds.这个问题听起来很简单。 I'm trying to use a raycast to determine if a player is on the ground in a 2D Unity Game.我正在尝试使用光线投射来确定玩家是否在 2D Unity 游戏中的地面上。 My code is this:我的代码是这样的:

RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 40, World);
if (hit.collider != null)
{
        print("touching");
}

I also have a public LayerMask World;我还有一个public LayerMask World; up above and the layer "World" is set on the object I want to detect collisions to.在上面,“世界”层设置在我想要检测碰撞的 object 上。 I'm pretty new to this so I'm not sure why the raycast isn't working.我对此很陌生,所以我不确定为什么光线投射不起作用。

Unchecking "Queries start within collider" in the setting solved the issue.取消选中设置中的“Queries start within collider”解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM