3

I am new to 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). Each of these has a Mesh Collider

  2. Canvas with a number of UI elements

  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:

 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. 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.

thanks

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

jason
  • 3,821
  • 10
  • 63
  • 120
  • Can you out a screenshot of what is blocking what? That would be helpful. – Programmer Jan 31 '17 at 03:32
  • Yes but I should add that all the items that are "blocking" the dice's onclick event are things that are really in the back ground. The dice sits in front of everything – jason Jan 31 '17 at 04:00
  • Are you certain that your `Raycast` is casting as intended? You possibly already know about this: https://docs.unity3d.com/ScriptReference/Debug.DrawRay.html - always handy for debugging raycasts. – Zze Jan 31 '17 at 04:11
  • Also, is there a particular reason you are not implementing `Monobehaviour.OnMouseDown() {}` on your dice instead of raycasting from the camera at them? – Zze Jan 31 '17 at 04:12
  • By Monobehaviour.OnMouseDown() {}, do you mean implementing the void OnMouseDown(){}? If so, I have tried that but the events never fire. I assume it had to do with the same reason that's causing this issue – jason Jan 31 '17 at 04:19
  • As for the raycaster comment, I am new to Unity. Im not sure how Raycast works or if it is doing what is intended. – jason Jan 31 '17 at 04:20
  • Read [this](http://stackoverflow.com/a/41392130/3785314) then use **#5** which says *"For 3D Object (Mesh Renderer/any 3D Collider)"*. All the other event callback functions from **#1** should also work. Let me know if you have any question. – Programmer Jan 31 '17 at 04:29
  • Great, thanks for the resource! – jason Jan 31 '17 at 04:42
  • Anytime. Did you try it? Your new question indicates that you did not. – Programmer Jan 31 '17 at 05:12
  • Yes. I am not sure what the issue was but I removed the already existing meshes/coliders and re-added them. This seems to have done the trick.My new question is about moving and object on a plane but only in 2 dimensions. Thanks again! – jason Jan 31 '17 at 13:11

1 Answers1

1

Put the dice on different layer and use layermask with your Raycast. Here is the documentation.

Dávid Florek
  • 552
  • 6
  • 17