垂直拖动手指滚动背景

时间:2019-05-28 18:41:16

标签: c# android unity3d touch

我想更改代码而不是滚动背景本身,而是通过垂直拖动手指来滚动。

private float speed = 0.5f;
Vector2 startPos;

void Start () {

    startPos = transform.position;

}

void Update () {

    transform.Translate((new Vector2(0, -1)) * speed * Time.deltaTime);

    if (transform.position.y < -19)
    {

        transform.position = startPos;

    }

}

1 个答案:

答案 0 :(得分:0)

使用Input.touchCountInput.GetTouchtouch.deltaPosition来检测触摸的距离。

您可以忽略deltaTime,因为它已经表示为自上一帧以来已拖动了多少屏幕。

void Update () {
    if (Input.touchCount > 0 ) 
    { 
        Touch touch = Input.GetTouch(0);

        float yMove = touch.deltaPosition.y / Screen.height;

        transform.Translate(Vector2.up * yMove * speed);

        if (transform.position.y < -19)
        {
            transform.position = startPos;
        }
    }

}