3
\$\begingroup\$

I have a game object on screen represented by a cube (but say it was a quad or something else all the same).

How do I raycast to check intersections? Keeping in mind I may not be raycasting fora physical object, just a gameobject of any sort.

Another question I have is, how can you raycast in Unity3D just to check against an intersection against anything in general (say the first gameobject to be hit)?

\$\endgroup\$
2
  • 4
    \$\begingroup\$ Physics.Raycast. Your object needs to have a collider. Depending on what you're doing a Rigidbody.SweepTest may also be useful. \$\endgroup\$ Commented Sep 25, 2011 at 12:19
  • 1
    \$\begingroup\$ Consider using trigges instead of raycasts, as triggers perform much better than recurring raycasts. \$\endgroup\$
    – jonas
    Commented Sep 27, 2011 at 9:10

2 Answers 2

2
\$\begingroup\$

If you want to stay with Raycasting, when you load your object, keep track of the faces in some way. Then raycast to the triangles instead of the whole object. This greatly improves your accuracy of your raycast, also built in an algorith that improves the raycasting technique

\$\endgroup\$
2
\$\begingroup\$

Raycasting in Unity is applied against colliders only, not gameobjects. In fact, gameobjects are nothing other than a storage of transform info. If you want raycasting against meshes (MeshRenderer on a gameobject) you can use a meshcollider.

Raycasting, especially against meshes are very expensive so, basic colliders are designed for performance. Meshcolliders are expensive too but the cost can be reduced by using Convex flag = true. It generates a simplified mesh from the original mesh but you should make sure if it's close enough to your original mesh.

Without a mesh collider, you can't use the built-in raycast functions. You can implement your own function working against the real meshes (The mesh in MeshRenderer component) but you don't gain any benefit by doing this. Because raycasting is implemented for you in unity already.

Just add a meshcollider to every object you want to raycast against. By the way, you can use the layer system to be able to filter some objects out when you are raycasting.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .