//一條從主相機到屏幕點擊點的射線ide
Ray ray = Camera.Main.ScreenPointToRay(Input.mousePosition);orm
RaycastHit hit;it
//發射一條射線返回相關信息(如需設定檢測所需層級,距離必定要加!!!)io
//CanRay爲定義的LayerMask層,如沒法手動添加選擇,可代碼ast
// int CanRay=1 << 10 ;form
/*transform
1 << 10 打開第10的層。
~(1 << 10) 打開除了第10以外的層。
~(1 << 0) 打開全部的層。
(1 << 10) | (1 << 8) 打開第10和第8的層。nw
*/移動
if (Physics.Raycast(ray, out hit, 1000, CanRay))
{
if (hit.transform.gameObject.name == "renwu")
{
print("移動射線檢測打到了人物");
}co
//當父子物體都有碰撞器時,如需檢測子物體,需將父物體層級關閉,或只打開子物體層級
//但如需獲取子物體信息時,需用hit.collider.name(名字),如用hit.transform.name時,返回的是父物體名字
}