【Unity筆記】經典的鼠標點擊射線檢測碰撞


void Update (){
    if(Input.GetMouseButton(0)){
        //從攝像機發出到點擊坐標的射線
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;
        if(Physics.Raycast(ray,out hitInfo)){
            //划出射線,只有在scene視圖中才能看到
            Debug.DrawLine(ray.origin,hitInfo.point);
            GameObject gameObj = hitInfo.collider.gameObject;
            Debug.Log("click object name is " + gameObj.name);
            //當射線碰撞目標為boot類型的物品,執行拾取操作
            if(gameObj.tag == "boot"){
                Debug.Log("pickup!");
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM