[Unity3D]射線碰撞檢測+LayerMask的使用
阿新 • • 發佈:2019-01-02
usingUnityEngine;
usingSystem.Collections;
publicclassLayerMaskTest : MonoBehaviour {
//Usethisforinitialization
voidStart ()
{
mainCrma = Camera.main;
mask = 1<<( LayerMask.NameToLayer("cube"));//例項化mask到cube這個自定義的層級之上。
}
//privateLayerMaskmask1<<10;
privateLayerMaskmask;
privateCameramainCrma;
private RaycastHitobjhit;
privateRay_ray;
voidUpdate ()
{
if (Input.GetMouseButtonDown(0))
{
_ray=mainCrma.ScreenPointToRay(Input.mousePosition);//從攝像機發出一條射線,到點選的座標
Debug.DrawLine(_ray.origin,objhit.point,Color.red,2);//劃出射線,只有在scene檢視中才能看到
if (Physics.Raycast (_ray,outobjhit, 100, mask.value))
{
GameObject gameObj = objhit.collider.gameObject;
Debug.Log("Hit objname:"+gameObj.name+"--Hit objlayerName:"+LayerMask.LayerToName (10));
}
}
}
}
usingSystem.Collections;
publicclassLayerMaskTest : MonoBehaviour {
//Usethisforinitialization
voidStart ()
{
mainCrma = Camera.main;
mask = 1<<( LayerMask.NameToLayer("cube"));//例項化mask到cube這個自定義的層級之上。
}
//privateLayerMaskmask1<<10;
privateLayerMaskmask;
privateCameramainCrma;
private
privateRay_ray;
voidUpdate ()
{
if (Input.GetMouseButtonDown(0))
{
_ray=mainCrma.ScreenPointToRay(Input.mousePosition);//從攝像機發出一條射線,到點選的座標
Debug.DrawLine(_ray.origin,objhit.point,Color.red,2);//劃出射線,只有在scene檢視中才能看到
if (Physics.Raycast (_ray,outobjhit, 100, mask.value))
{
GameObject
Debug.Log("Hit objname:"+gameObj.name+"--Hit objlayerName:"+LayerMask.LayerToName (10));
}
}
}
}