0
\$\begingroup\$

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....

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

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);
            }
        }

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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