handle mousedrag() for two different game objects individually in 2D environment

I am developing a pong game for android mobiles having 2 slider and want to assign movement of individual slider corresponding to its current position

I tried some code for the movement of these sliders using OnMouseDrag();

void OnMouseDrag()
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector2(transform.position.x, mousePos.y);

}


I got the movement for the sliders but problem is that, I can only slide one slider at a time But I need to move both the sliders at the same time using both hands because this game is getting developed for android mobiles environment.

• Maybe mouse isn't the best solution for android mobiles. Have you tried using Input.touches? – Adrian Krupa Jul 16 '15 at 8:47
• no don't have proper idea to use Input.touches – Santosh Yadav Jul 16 '15 at 8:50

The best way to handle this, is to divide your screen in 2 and check to see if someone is holding down a finger in that area. An easy way to do this, is to add two large Box Colliders on each side of your game scene, which will serve as a hit detection for Touches.

Check if the Collider is pressed, and then simulate the corresponding paddle with that touch.

This is just some untested sample code you can play around with, can be greatly improved on:

public BoxCollider2D Paddle1Collider;

void Update()
{

foreach (Touch touch in Input.touches) {

RaycastHit2D[] hits;
Vector2 pos = Camera.main.ScreenToWorldPoint (touch.position);
hits = Physics2D.RaycastAll (pos, new Vector2 (0, 0), 0.01f);

for (int i = 0; i < hits.Length; i++) {
if (hits [i].collider == Paddle1Collider) {
}
else if (hits [i].collider == Paddle2Collider) {