Unity平滑触摸x坐标

时间:2015-12-28 20:09:11

标签: c# unity3d

我有一个2D手机游戏,我需要触摸和拖动对象。这是一个脚本(使用此脚本,对象无法顺利移动)。我希望移动物体在那个时候的手指位置。

public float speed;

    void  Update ()
    {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) 
        {

            Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;

            transform.Translate(touchDeltaPosition.x * speed, 0, 0);
        }

怎么办?

由于 亲切的问候

1 个答案:

答案 0 :(得分:1)

您不应在此处使用speed,因为它会毫不拖延地为您提供准确的位置。因此,请尝试删除speed 之类的transform.Translate(touchDeltaPosition.x, 0, 0);

<强>更新

您也可以使用Vector3.MoveTowards。试一试

void  Update ()
    {
        if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) { //pomicanje trake po x-osi na touch screenu
            // pokret prsta od zadnjeg frejma
            Vector3 touchDeltaPosition = Input.GetTouch (0).deltaPosition;
            // Za x-os
            transform.position = Vector3.MoveTowards (transform.position, new Vector3 (Mathf.Clamp (touchDeltaPosition.x, -2.5f, 2.5f), transform.position.y, transform.position.z), 1);
        }
    }

而不是transform.Translate

它应该很好用。