- Home /
RaycastHit behind object
How I should get raycastHit behind rigidbody? Box on this picture has rigidbody, but raycast should get "hit.point" from the wall, not box.
Answer by AlucardJay · Mar 02, 2013 at 09:57 AM
Sorry only time for a very quick comment.
This can be done using LayerMask.
http://docs.unity3d.com/Documentation/Components/Layers.html
In the Inspector, click the dropdown box next to the variable myLayerMask. Tick to select the layers in which you want the raycast to work. Your colliders need to have their layers set to whatever you want. If you have everything on the same layer this will notwork.
// JavaScript example.
var myLayerMask : LayerMask;
function Update()
{
var hit : RaycastHit;
// Does the ray intersect any objects excluding the myLayerMask layer
if ( Physics.Raycast( transform.position, transform.forward, hit, Mathf.Infinity, myLayerMask ) )
{
Debug.DrawRay( transform.position, transform.forward * hit.distance, Color.yellow );
Debug.Log( "Did Hit " + hit.collider.gameObject.name );
}
else
{
Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) *1000, Color.white);
Debug.Log( "Did not Hit" );
}
}
Answer by Hybris · Mar 02, 2013 at 09:49 AM
If you put the box in the ignoreraycast layer, the raycasts will ignore the box completely and go right through it.
Great answer. No coding needed and fixed my issue. Cheers ;-)