How do I move an object up and down by dragging it?

27 views Asked by At

I have a 2D mobile pong game in Unity. I want to move the paddle up and down with finger drag motion. I want the paddle to have a movement speed. I was able to do this in the codes I tried, but the paddle moves at the same speed as my finger and teleports to the position where my finger is. How can I do?

foreach (Touch touch in Input.touches)
        {
            Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
            Vector2 myPosition = rb.position;
            if(Mathf.Abs(touchPosition.x - myPosition.x) <=2){
                myPosition.y = Mathf.Lerp(myPosition.y, touchPosition.y,10);
                myPosition.y = Mathf.Clamp(myPosition.y, -3.7f,3.7f);
                rb.position = myPosition;
            }

        }
0

There are 0 answers