- void MobilePick()
- {
- if (Input.touchCount != 1 )
- return;
- if (Input.GetTouch(0).phase == TouchPhase.Began)
- {
- RaycastHit hit;
- Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
- if (Physics.Raycast(ray, out hit))
- {
- Debug.Log(hit.transform.name);
- //Debug.Log(hit.transform.tag);
- }
- }
- }
- void MousePick()
- {
- if(Input.GetMouseButtonUp(0))
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit))
- {
- Debug.Log(hit.transform.name);
- //Debug.Log(hit.transform.tag);
- }
- }
- }
复制代码在 unity3d中,选中物体还有一个条件,就是物体能发生碰撞。这个参数就是碰撞器Collider,Collider是发生物理碰撞的基本条件。 所以如果无法选中物体时,要检查是否物体加了碰撞器。 方法如下: - GameObject gameObject = (GameObject)Instantiate(...);
- gameObject.name = "game_object";
- gameObject.AddComponent<MeshCollider>();
复制代码 |