【问题标题】:Raycast that ignore certain Game Objects忽略某些游戏对象的 Raycast
【发布时间】:2016-07-21 15:46:57
【问题描述】:

PlayerController

public LayerMask groundLayer;

    void LookRotation()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hitInfo;
    if (Physics.Raycast(ray, out hitInfo, groundLayer))
    {
        destination = new Vector3(hitInfo.point.x, transform.position.y, hitInfo.point.z);
        //Look at mouse position
        transform.LookAt(destination);
    }
}

我在让我的 Raycast 忽略我的玩家或只检测地面时遇到问题。在实际的统一检查器中,我将LayerMask 设置为我的Ground Layer,并且使用我当前的代码,它只会检测到位于Ground Layer 中的地面,尽管玩家仍然会干扰Raycast,即使它是不在Ground Layer 中。我需要 Raycast 完全忽略玩家,即使这是通过将玩家添加到 Ignore Raycast 层来实现的,但我不能这样做,因为玩家需要被不同的 Raycast 检测到。所有这些代码都出现在我的PlayerController 脚本中,因为它用于确定我的玩家轮换,我不确定这是否可能是玩家干扰 Raycast 的原因。提前干杯!

【问题讨论】:

  • 谢谢乔,我想出了我的第二个问题。
  • 太棒了!回头见……

标签: c# unity3d


【解决方案1】:

(1) 您是否以正确的方式使用图层 MASK?

更像是这样……

int layerMaskYourHero = 1 << LayerMask.NameToLayer("YourHero");

然后

    RaycastHit2D hit = Physics2D.Raycast(
        laserEmittingPoint.position, Vector2.left,
        castLength,
        layerMaskYourHero);

(2) 说,你用的是物理矩阵吧?

进入编辑 -> 项目设置 -> 物理

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多