Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)?

share|improve this question
4  
Physics.Raycast. Your object needs to have a collider. Depending on what you're doing a Rigidbody.SweepTest may also be useful. –  Jonathan Connell Sep 25 '11 at 12:19
1  
Consider using trigges instead of raycasts, as triggers perform much better than recurring raycasts. –  jonas Sep 27 '11 at 9:10
1  
@Jonathan Why did you not post that as an answer!? All my upvote are belong to you! –  Anko Dec 3 '11 at 18:39
add comment

2 Answers

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.