统一屏幕三角洲到世界三角洲

时间:2017-06-12 11:31:10

标签: c# unity3d

我想将用户触摸增量位置转换为世界中游戏对象的增量位置,是否有实用工具?

   //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);


    }

请注意,我正在使用具有正交模式的相机构建2D游戏

1 个答案:

答案 0 :(得分:0)

由于您的对象似乎不会随时间变化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;
}

希望这有帮助,