2017-06-12 73 views
0

我想將用戶觸摸三角洲位置轉換爲世界上的遊戲對象的三角洲位置,有沒有這樣做?統一屏幕三角洲世界三角洲

//drag 
    if (duringmove && (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)) 
    { 
     Debug.Log("will move " + progressTrans.gameObject.name); 
     endPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position); 

     //should convert te deltaPos here 
     progressTrans.localPosition += Vector3.right * Input.GetTouch(0).deltaPosition.x; 
     progressTrans.localPosition = new Vector3(Mathf.Clamp(progressTrans.localPosition.x, mostLeft, mostRight), progressTrans.localPosition.y, progressTrans.localPosition.z); 


    } 

請注意,我用建設與正字模式

+0

如果您的2D遊戲是「內部」_Screen空間 - 覆蓋_畫布,您的'Input.GetTouch(0).deltaPosition'值應該與您要應用的'localPosition' delta相同。也不知道爲什麼'endPos = [']'線在這裏? – Kardux

+0

@Kardux感謝您的答覆,gameobject不在畫布中,endPos是爲了進一步使用。 – armnotstrong

回答

0

攝像頭的2D遊戲因爲它似乎你的對象不會有它的transform.position.z隨時間變化的,你可以使用這樣的事情:

//drag 
if (duringmove && (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)) 
{ 
    Debug.Log("will move " + progressTrans.gameObject.name); 

    Vector3 touchPosition = Input.GetTouch(0).position; 
    touchPosition.z = progressTrans.position.z; 

    // World touch position 
    endPos = Camera.main.ScreenToWorldPoint(touchPosition); 
    // To local position 
    progressTrans.InverseTransformPoint(endPos); 

    endPos.x = Mathf.Clamp(endPos.x, mostLeft, mostRight); 
    endPos.y = progressTrans.localPosition.y; 
    endPos.z = progressTrans.localPosition.z; 

    // If you need the delta (to lerp on it, get speed, ...) 
    Vector3 deltaLocalPosition = endPos - progressTrans.localPosition; 

    progressTrans.localPosition = endPos; 
} 

希望這有助於