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!"); } } } }