2014-08-28 3 views
0

개체를 Y 축으로 끌기위한 스크립트를 만들고 있습니다. 거의 끝났지 만 문제가 하나 있습니다. 개체를 클릭하면 개체의 중심이 마우스 위치로 이동 한 다음 중심을 항상 마우스 위치로 드래그합니다. 예를 들어 스프라이트의 아래쪽을 잡고있을 때 어떻게 만들 수 있습니까? 스프라이트의 중심은 그대로 있고 마우스 위치로 이동하지 않습니다. 내 스크립트는 다음과 같습니다.Unity3d 손가락 문제가있는 객체를 끌기

public Vector3 screenPoint; 
    public Vector3 offset; 

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

    void OnMouseDrag(){ 
     Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 
     Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint), offset; 
     transform.position = new Vector3(transform.position.x, curPosition.y, transform.position.z); 
    } 

고맙습니다.

답변

1

귀하의 curScreenPoint이 아직 픽셀 좌표에 있기 때문입니다. OnMouseDown에서 한 것은 맞았습니다.

변경이 라인 : 여기에

Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, 
            Input.mousePosition.y, 
            screenPoint.z); 

: 또한

Vector3 curScreenPoint = Camera.main.ScreenToWorldPoint (new Vector3 (
                 Input.mousePosition.x, 
                 Input.mousePosition.y, 
                 screenPoint.z) 
                 ); 

내가 당신을 아주하는지 이해하지를 그래서 당신은 또한 픽셀이 OnMouseDrag의 화면 공간 좌표 변환해야 다음 줄에서 수행 :

Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint), offset; 

하지만 이것은 단지 mo가되기를 원하기 때문에 이와 같아야합니다. Y :

Vector3 curPosition = new Vector3(curScreenPoint.x, 
            curScreenPoint.y + offset.y, 
            curScreenPoint.z); 
+0

질문이 하나 더 있습니다. 어떻게하면 움직임을 제한하고 최대 탑 스폿과 최대 봇 스팟을 설정하여 객체가 하나에서 다른 것으로 이동할 수 있도록 할 수 있습니까? – filipst

+0

[Mathf.Clamp] (http://docs.unity3d.com/ScriptReference/Mathf.Clamp.html)를 사용하여 최대 및 최소 좌표를 제한 할 수 있습니다. –

+0

대단히 감사합니다 :) – filipst