Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

enter image description here

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.

please Help....

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

1 Answer 1

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;
public BoxCollider2D Paddle2Collider;


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) {
                Paddle1.Move(pos);
            }
            else if (hits [i].collider == Paddle2Collider) {
                Paddle2.Move(pos);
            }
        }

    }
}
share|improve this answer
    
not able to divide the screen in to two parts using collider...any help please –  Santosh Yadav Jul 17 at 5:58
    
@SantoshYadav Can you give me more information as to why you cannot do this? –  Jon Jul 17 at 10:24
    
actually i am new for 2D game development and don't know how to add an empty collider means without any game object. –  Santosh Yadav Jul 23 at 5:55
    
Create a blank gameObject and add a collider to it ;) –  Jon Jul 23 at 10:28
    
actually i did that but it disturbing the position of both sliders –  Santosh Yadav Jul 23 at 10:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.