- Home /
 
 
               Question by 
               Leandrolo15 · Apr 27, 2018 at 09:28 AM · 
                raycastmultiplayerplayer movementtouch controlstouchscreen  
              
 
              Touch controls for Pong based video games for Android
I am making a pong based video game for Android devices and I am having trouble with touch controls. I have tried OnMouseDown() and OnMouseDrag(), but it only work with one of the paddles (I am using a generic script for both paddles). I am a little new to Unity and I have never done anything related to touch controls. Can anyone help me? This is the code I have right now:
 Vector3 dist;
 Vector3 startPos;
 float posX;
 float posZ;
 float posY;
 void OnMouseDown() {
     startPos = transform.position;
     dist = Camera.main.WorldToScreenPoint(transform.position);
     posX = Input.mousePosition.x - dist.x;
     posY = Input.mousePosition.y - dist.y;
     posZ = Input.mousePosition.z - dist.z;
 }
 void OnMouseDrag() {
     float disX = Input.mousePosition.x - posX;
     float disY = Input.mousePosition.y - posY;
     float disZ = Input.mousePosition.z - posZ;
     Vector3 lastPos = Camera.main.ScreenToWorldPoint(new Vector3(disX, disY, disZ));
     float Zposition = lastPos.z + (speed * Time.deltaTime);
     if (Zposition <= -4.15f) {
         Zposition = -4.15f;
     } else if (Zposition >= 4.15f) {
         Zposition = 4.15f;
     }
     if (CompareTag ("PlayerBar")) {
         transform.position = new Vector3 (startPos.x, startPos.y, Zposition * changeDirection1);
     } else if (CompareTag ("Player2Bar")) {
         transform.position = new Vector3 (startPos.x, startPos.y, Zposition * changeDirection2);
     }
 }
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Raycasting UI elements - Without using Event Triggers 0 Answers
How to make basic touch screen controls 1 Answer
Make touch buttons stop working after finger slides off. 1 Answer
Touch controls for Pong game for Android devices 0 Answers
Null reference exception on Screenpointtoray (Multiplayer) 1 Answer