- Home /
How Can I Drag the Object With Touch ? (Mobile)
Hello,
I'm making a pong game for iPhone and I'm stuck at one point and in need of assistance! I want to drag the Player Object just by touching on IT, not anywhere else. I can move it by clicking anywhere on the screen, but that makes it impossible to implement 2-player game. I have 2 individual player sprites and I want them both can be controlled by dragging separately on the same scene.
Thank you!
Answer by Exalia · Jan 20, 2014 at 10:42 PM
Hi there, I made a game today myself using this script that is similar your pong game.
 using UnityEngine;
 using System.Collections;
 
 //Class to control rackets via touch
 public class TouchControl : MonoBehaviour 
 {
     //Public Variables
     public GameObject player1;
     public GameObject player2;
     //A modifier which affects the rackets speed
     public float speed;
     //Fraction defined by user that will limit the touch area
     public int frac;
 
     //Private Variables
     private float fracScreenWidth;
     private float widthMinusFrac;
     private Vector2 touchCache;
     private Vector3 player1Pos;
     private Vector3 player2Pos;
     private bool touched = false;
     private int screenHeight;
     private int screenWidth;
     // Use this for initialization
     void Start () 
     {
         //Cache called function variables
         screenHeight = Screen.height;
         screenWidth = Screen.width;
         fracScreenWidth = screenWidth / frac;
         widthMinusFrac = screenWidth - fracScreenWidth;
         player1Pos = player1.transform.position;
         player2Pos = player2.transform.position;
     }
     
     // Update is called once per frame
     void Update () 
     {
 //If running game in editor
 #if UNITY_EDITOR
         //If mouse button 0 is down
         if(Input.GetMouseButton(0))
         {
             //Cache mouse position
             Vector2 mouseCache = Input.mousePosition;
             //If mouse x position is less than or equal to a fraction of the screen width
             if (mouseCache.x <= fracScreenWidth)
             {
                 player1Pos = new Vector3(-7.5f, 0.5f, Mathf.Clamp(mouseCache.y / screenHeight * speed, 0, 8));
             }
             //If mouse x position is greater than or equal to a fraction of the screen width
             if(mouseCache.x >= widthMinusFrac)
             {
                 player2Pos = new Vector3(7.5f, 0.5f, Mathf.Clamp(mouseCache.y / screenHeight * speed, 0, 8));
             }
             //Set touched to true to allow transformation
             touched = true;
         }
 #endif
         //If a touch is detected
         if (Input.touchCount >= 1)
         {
             //For each touch
             foreach (Touch touch in Input.touches)
             {
                 //Cache touch position
                 touchCache = touch.position;
                 //If touch x position is less than or equal to a fraction of the screen width
                 if (touchCache.x <= fracScreenWidth)
                 {
                     player1Pos = new Vector3(-7.5f, 0.5f, Mathf.Clamp(touchCache.y / screenHeight * 8, 0, 8));
                 }
                 //If mouse x position is greater than or equal to a fraction of the screen width
                 if(touchCache.x >= widthMinusFrac)
                 {
                     player2Pos = new Vector3(7.5f, 0.5f, Mathf.Clamp(touchCache.y / screenHeight * 8, 0, 8));
                 }
             }
             touched = true;
         }
     }
 
     //FixedUpdate is called once per fixed time step
     void FixedUpdate()
     {
         if (touched)
         {
             //Transform rackets
             player1.transform.position = player1Pos;
             player2.transform.position = player2Pos;
             touched = false;
         }
     }
 }
 
I'm currently using this script so I know it works. Let me know if you use it and run into issues
I get the point you want to make, but there is a little problem about it. I want to move the object, by only dragging the object itself, not a whole area. Is there any way to make this happen without splitting the screen or something ?
Thank you for your response!
I get the point you want to make, but there is a little problem about it. I want to move the object, by only dragging the object itself, not a whole area. Is there any way to make this happen without splitting the screen or something ?
Thank you for your response!
Sure I'll try again haha
This method requires you to set up a collider on your pong racket thingies :)
 foreach(Touch touch in touches)
 {
  Ray ray = Camera.main.ScreenPointToRay(Input.touch.position);
  RaycastHit hit;
 
  if(Physics.Raycast(ray, out hit, 100))
  {
  //$$anonymous$$ight need to set X and Z depending on how your game is set up as touch.position is a 2D Vector
   sprite.transform.position = touch.position;
  }
 }
What's happening here is that a Ray is cast from the camera whenever a touch is made, if the Ray collides with something within 100 units (Z units I believe) it will set the sprite position to that position.
You might want to do a check to make sure your ray collided with your pong sprite or something
When I use this code, it gives an error like this. " The name 'touches' does not exist in the current context.
EDIT: Btw here's a screenshot of my object's properties.

This should fix your error
 if (Input.touchCount >= 1)
 {
  foreach(Touch touch in Input.touches)
  {
   Ray ray = Camera.main.ScreenPointToRay(touch.position);
   RaycastHit hit;
   if(Physics.Raycast(ray, out hit, 100))
  }
 }
 
Answer by robertbu · Jan 20, 2014 at 11:01 PM
Here is one solution that uses the OnMouse*() callbacks. It will work 'as is' for mobile, but it is not the most efficient method.
http://answers.unity3d.com/questions/566327/drag-object-relative-to-camera.html
There are lots of posts with drag and drop scripts. Most use Raycasting and the mouse. It is pretty straightforward to convert from mouse to touch. You will find a mouse solution here:
http://answers.unity3d.com/questions/498396/how-to-click-and-drag-an-object-at-full-speed.html
Answer by LostInCode404 · Jun 29, 2014 at 09:08 AM
I've recently made a tutorial on touch and drag objects on my blog that may help you for your purpose. link - http://newtonians3d.blogspot.in/2014/06/a-simple-and-efficient-touch-and-drag.html I hope this was helpful.
Your answer
 
 
             Follow this Question
Related Questions
Dragging UI Image by touch 3 Answers
Mobile touch dragging from mouse dragging 1 Answer
Finger gesture works/fails on different devices? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                