简体   繁体   English

Unity3D可防止与鼠标事件发生冲突

[英]Unity3D prevent clashes with mouse events

I am new to Unity3D. 我是Unity3D的新手。 I have a scene composed of a number of objects: 我有一个由许多对象组成的场景:

  1. A prefab that makes a room (mesh plane for the floor, 4 mesh planes for the walls). 制作房间的预制件(地板的网格平面,墙壁的4个网格平面)。 Each of these has a Mesh Collider 每个都有一个网格对撞机

  2. Canvas with a number of UI elements Canvas具有许多UI元素

  3. Inside the prefab room I created a table, comprised of a Cylinder (for the table base), a cube for the table top, four cubes that create walls around the edge of the table top to give it a lip, and on the table top is a plane (this is the table "felt" and also has a box collider and Rigid Body). 在预制房间内,我创建了一个桌子,包括一个圆柱体(用于桌面底座),一个用于桌面的立方体,四个立方体,围绕桌面边缘创建墙壁,使其具有唇部,并在桌面上是一架飞机(这是“感觉”的表,还有一个箱式对撞机和刚体)。 These all have Colliders. 这些都有碰撞器。 This is all made into a prefab. 这都是预制件。

  4. On the table top I have some prefab cubes (dice). 在桌面上我有一些预制立方体(骰子)。 These have a rigid body and collider. 它们有一个刚体和对撞机。

The problem I am having is that the mouse events do not register for the dice (they have a script attached). 我遇到的问题是鼠标事件没有注册骰子(他们附加了一个脚本)。 On the Update event, I have this code: 在Update事件中,我有这个代码:

 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("--------------");
            }
        }

I can see that even though I click on the dice, other objects are getting in the way (ie. the walls of the room, the table walls, table top, etc.). 我可以看到,即使我点击骰子,其他物体也会挡住(即房间的墙壁,桌面,桌面等)。

So, how do I over come this? 那么,我该如何过来呢? Other than the UI elements, the only objects that need to have mouse events are the dice. 除了UI元素之外,唯一需要具有鼠标事件的对象是骰子。 Is there some way to not register mouse events for everything else? 有没有办法不为其他一切注册鼠标事件? Oh, and I'm writing this in C# and this is a 3D project. 哦,我在C#写这个,这是一个3D项目。

thanks 谢谢

在这里你可以看到点击骰子的结果。它注册了TableBase。如果我将骰子向上移动,远离桌子,后墙在骰子后面,那么后墙将被注册

Put the dice on different layer and use layermask with your Raycast. 将骰子放在不同的图层上,并使用您的Raycast使用图层蒙版。 Here is the documentation. 这是文档。

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

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