c# - Unity3D防止与鼠标事件发生冲突

标签 c# unity3d

我是 Unity3D 的新手。我有一个由许多对象组成的场景:

  1. 构成房间的预制件(地板的网格平面,墙壁的 4 个网格平面)。每个都有一个 Mesh Collider

  2. 带有大量 UI 元素的 Canvas

  3. 在预制房间内,我创建了一张 table ,包括一个圆柱体(用作 table 底座)、一个用作桌面的立方体、四个立方体,这些立方体在桌面边缘周围形成墙壁以形成边缘,在桌面上是一个平面(这是“感觉”的 table ,还有一个盒子对撞机和刚体)。这些都有碰撞器。这都是预制件。

  4. 在桌面上我有一些预制立方体(骰子)。它们具有刚体和对撞机。

我遇到的问题是鼠标事件没有为骰子注册(它们附有脚本)。在更新事件中,我有这段代码:

 if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                Debug.Log("Name = " + hit.collider.name);
                Debug.Log("Tag = " + hit.collider.tag);
                Debug.Log("Hit Point = " + hit.point);
                Debug.Log("Object position = " + hit.collider.gameObject.transform.position);
                Debug.Log("--------------");
            }
        }

我可以看到,即使我点击了骰子,其他物体也会挡道(即房间的墙壁、 table 墙壁、桌面等)。

那么,我该如何克服呢?除了 UI 元素,唯一需要鼠标事件的对象是骰子。有什么方法可以不为其他一切注册鼠标事件吗?哦,我是用 C# 编写的,这是一个 3D 项目。

谢谢

here you can see the result of clicking on the dice.  It registers the TableBase.  If I move the dice up, away from the table, where the back wall is behind the dice, then the back wall will be registered

最佳答案

将骰子放在不同的图层上,并在 Raycast 中使用图层蒙版。 这是 documentation.

关于c# - Unity3D防止与鼠标事件发生冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41949194/

相关文章:

c# - 如何将 IntPtr/Int 转换为 Socket?

C# 远程调试器 - 无法找到或打开 PDB 文件

c# - 哪个用户引起了 FileSystemWatcher 事件?

c# - 部署步骤中发生错误 'Retract Solution' : <CLASS> cannot be deserialized because it does not have a public default constructor

c# - 3D 体素网格视线 Bresenham 算法

c# - IServiceCollection 不包含定义 AddJsEngineSwitcher

c# - 在 Unity3D 中按名称加载脚本

c# - 在 Unity 中更改按钮的文本

xcode - 在通用架构项目上使用 -ObjC 链接器标志会导致 Mach-O 链接器错误?

c# - Client-Unet 中未收到广播