0
\$\begingroup\$

I have a game which involves clicking to select some objects. Each of the objects has a mesh collider attached to it so that objects can be clicked, handled by the following:

if (Input.GetMouseButtonDown(0)){ // if left button pressed...



    // Find hit object
    Ray ray = task_scene.main_camera.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit)){
        execute_some_function();

    }
}

Now this works fine when I run the game in the editor. However, when I build and run in the browser some objects are not being selected when clicked, and it seems to be objects that are created from particular meshes. I have tried:

  • changing the mesh import settings to generate colliders but this doesn't seem to do anything
  • changing Prebake Collision Meshes in build settings, which again doesn't solve the problem
  • Changing mesh collider cooking option to both 'everything' and 'none'
  • Setting 'Queries Hit Backfaces' to true in physicsmanager settings
  • making setting the meshes to convex, which does then work in the build but is not ideal.

EDIT: I've noticed that the raycasting is actually working for the problem objects but only from particular directions. Is this because, according to the manual: 'Faces in collision meshes are one-sided. This means objects can pass through them from one direction, but collide with them from the other.'

Any thoughts on what might be causing this?

\$\endgroup\$
  • \$\begingroup\$ Hey. It´s not an exact answer for your question but did you try using the method OnMouseDown() instead? If for any reason using a raycast is not required you could use that method \$\endgroup\$ – Pascal Lohscheidt Jun 11 at 8:30
  • \$\begingroup\$ Thanks for the suggestion. I haven't tried this, but this would require adding a script containing OnMouseDown() to each object, right? This would be a bit of a pain as I currently have one clean and efficient script handling everything. Also, does OnMouseDown() not use raycasting anyway? \$\endgroup\$ – A. Bollans Jun 12 at 11:23
  • \$\begingroup\$ Yeah didn´t think of that. My fault ;). \$\endgroup\$ – Pascal Lohscheidt Jun 13 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.