2013-02-13 6 views
1

Unity3d에서 카드 게임을 만들고 있습니다. C#을 사용하여 프로그래밍 방식으로 게임 객체로 카드를 만들었습니다. 각 객체 (카드)를 마우스 버튼 클릭으로 움직이는 방법을 알고 싶었고, Raycast 콜라이더를 사용해 보았습니다. 그러나 작동하지 않습니다. 메쉬와 함께 전체 커버 인 부모 GameObject에 액세스하려고 시도하고 있으며 콜 아이더 객체/구성 요소를 통해 하위 GameObject에 액세스하려고합니다 (위치 이동 전용).이 문제를 해결할 수있는 쉬운 방법이 있습니까? 이 모든 것을 다른 방법으로하는 더 좋은 방법이 있습니까?스크립트에서 Collider 's GameObject에 액세스하려면 어떻게해야합니까?

는 업데이트 :

if (Input.GetMouseButton (0)) {      
    RaycastHit hit = new RaycastHit(); 
    Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
    if (Physics.Raycast (ray, out hit)) { 
     print (hit.collider.gameObject.name); 
    } 
} 
+0

사용하신 레이캐스터 충돌 코드를 게시 하시겠습니까? –

+0

예. 다음 코드를 사용했습니다. (Input.GetMouseButton (0)) { RaycastHit hit = new RaycastHit(); Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); // **** if (Physics.Raycast (ray, out hit)) { print (hit.collider.gameObject.name); } } – Ananya

답변

0

Input.GetMouseButton(0)Input.GetMouseButtonDown(0)해야한다.

사용자가 클릭하는 첫 번째 프레임에만 등록하는 Input.GetMouseButtonDown(0)에 반대하여 마우스가 다운 된 모든 프레임을 등록하는 Input.GetMouseButton(0)을 사용하려고합니다.

예제 코드 : 그게 해결되지 않으면

if (Input.GetMouseButtonDown(0)) 
    print ("Pressed"); 
else if (Input.GetMouseButtonUp(0)) 
    print ("Released"); 

if (Input.GetMouseButton(0)) 
    print ("Pressed"); 
else 
    print ("Not pressed"); 

, 나는 aswell이 문제 우연히 if (Physics.Raycast (ray, out hit, 1000)) {

0

으로 if (Physics.Raycast (ray, out hit)) {를 교체하려고, (이 대신 시도 Btw u는 GetMouseButtonUp을 대신 사용할 수 있음)

if (Input.GetMouseButtonDown (0)) 
{      
RaycastHit hit = new RaycastHit(); 
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
if (Physics.Raycast (ray, out hit)) { 
    print (hit.collider.transform.gameObject.name); 
} 

}

어떤 식 으로든 Transform을 통해 액세스 할 수있는 방법은 나를위한 트릭입니다. 그리고 당신은 부모에 액세스하려면 : 내가 도울 수

// You either access it by index number 
hit.collider.transform.getChild(int index); 
//Or you could access some of its component (I prefer this method) 
hit.collider.GetComponentInChildren<T>(); 

희망 :

hit.collider.transform.parent.gameObject; 

지금 아이는 까다로운 조금이다. 건배!