Unity: Object is not being detected by raycast for highlighting

半城伤御伤魂 提交于 2019-12-07 02:38:30
Denis Potapenko

The reason was that "femur_left_1_7_dist/default" and "femur_left_1_7_prox/default" has no colliders. So there are two ways to resolve this issue:

  1. In project view select "femur_left_1_7_dist" and "femur_left_1_7_prox", and in inspector in Import settings choose "Generate Colliders" and press "Apply" button:


    OR
  2. Select "femur_left_1_7_dist/default" in the scene and press "Component/Physics/Box Collider"; see the result here: https://dl.dropboxusercontent.com/u/20023505/stackoverflow_forum/s_fix.zip

There could be several things causing this but here are a few things to check for.

  • Make sure your GameObject has a Collider Component attached to it.
  • Make sure the GameObjects layer is not set to Ignore Raycast.
  • Make sure you are Raycasting form the right camera.

The tutorial seems to be using a default camera for the Raycast, make sure you have a Camera in the scene that has its Tag set to Main Camera.

I used your code in a scene with three cubes (room0 to room2) and a plane (flat) and it worked fine.

But your code is bit weird tough, especially the TransformObject class. This class should be a MonoBehaviour (a script) and added as a Component to the same GameObject than your AppRoot script.

You can even make it automatic by using the RequireComponentAttribute on your AppRoot class.

Now then, why isn't your code working in your case ?

  • Did you try to use the debugger while running your code, to see whether SelectObjectByMousePos() and SelectedObject got called at all?
  • It is possible that the material you wanted to use isn't working with your meshes: try using your script with cubes (like I did) instead of your .obj
  • Rewrite your code so that TransformObject is a MonoBehaviour script.
  • Check the collision matrix, maybe some layer to layer collision detection are disabled.

edit: does raycasting work with non-convex colliders?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!