2017-06-12 76 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; 
} 

希望这有助于