2016-11-20 1 views
2

저는 Unity에서 드래그 가능한 게임 객체를 만들기 위해 노력하고 있습니다. 마우스를 따라갈 수 있도록 지금 가지고 있습니다.마우스로 객체를 드래그하여 값을 스냅하십시오.

정확하게 필요한 것은 아니며 마우스가있는 곳으로 가려면 마우스 방향으로 0.375f 이동해야합니다.

나는 그것이 어떻게 작동 될지 상상할 수 없다 - 어떤 도움을 주시면 감사하겠습니다!

void OnMouseDown() 
    { 
     screenPoint = Camera.main.WorldToScreenPoint(transform.parent.position); 
     offset = transform.parent.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 
    } 

    void OnMouseDrag() 
    { 
     Vector3 curScreenPt = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 
     Vector3 curPos = Camera.main.ScreenToWorldPoint(curScreenPt) + offset; 
     transform.parent.position = new Vector3(curPos.x, curPos.y); 
    } 
+0

중요 : 요즘 드래그 앤 드롭과 비슷한 개념을 Unity에 구현하는 것이 믿기지 않습니다. 여기에 : http://stackoverflow.com/a/37473953/294884 – Fattie

+0

그래,하지만 afaik 그것의 UI가 - 전용, 나는 진짜로 작동하지 않습니다 그래서 게임 개체에 최선을 다하고 있습니다 – Fiffe

답변

2

신경 끄시 고, 내가 바보 야 : 이것은 내가 지금까지있어 것입니다.

void OnMouseDown() 
{ 
    screenPoint = Camera.main.WorldToScreenPoint(transform.parent.position); 
    offset = transform.parent.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 
} 

void OnMouseDrag() 
{ 
    Vector3 curScreenPt = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 
    Vector3 curPos = Camera.main.ScreenToWorldPoint(curScreenPt) + offset; 
    transform.parent.position = new Vector3(Mathf.Round(curPos.x/0.3175f) * 0.3175f, Mathf.Round(curPos.y/0.3175f) * 0.3175f); 
} 
+0

아무도 내 남자 여기 바보! – Fattie

관련 문제