- Home /
 
Drag GameObject with finger touch in SmartPhone
I have maybe a dumb question, but how do I drag a GameObject when I touch the screen? i'm new in Unity and i need to drag an object and make collide against other object.
Answer by Landern · Aug 01, 2016 at 02:59 PM
You need to check for touches, get the touch and update the transform of whatever object you're touching(hopefully).
Documentation of concern:
 if (Input.touchCount > 0) 
 {
     Touch touch = Input.GetTouch(0); // get first touch since touch count is greater than zero
     
     if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) 
     {
         // get the touch position from the screen touch to world point
         Vector3 touchedPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
         // lerp and set the position of the current object to that of the touch, but smoothly over time.
         transform.position = Vector3.Lerp(transform.position, touchedPos, Time.deltaTime);
     }
 }
 
              thanks a lot. I used the next code and kind of work for me but the problem that i got is that i use the same code for several sprites but when I pass over another sprite the sprite absorb the other one. how can i fix that?
using UnityEngine; using System.Collections;
public class movimiento : $$anonymous$$onoBehaviour {
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () 
 {
     //Gets the world position of the mouse on the screen        
     Vector2 mousePosition = Camera.main.ScreenToWorldPoint( Input.mousePosition );
     //Checks whether the mouse is over the sprite
     bool overSprite = this.GetComponent<SpriteRenderer>().bounds.Contains( mousePosition );
     //If it's over the sprite
     if (overSprite)
     {
         //If we've pressed down on the mouse (or touched on the iphone)
         if (Input.GetButton("Fire1"))
         {
             //Set the position to the mouse position
             this.transform.position = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
                 Camera.main.ScreenToWorldPoint(Input.mousePosition).y,
                 0.0f);
         }
     }
 }
 
                  }
I used this code and it works well, the only problem I have is that it moves the object along all three axis and I need it to move only on x and y. Do you maybe have a solution for that?
The code above works for the most part, however, if this is on a prefab, then it moves all of the instances simultaneously. Is there a modification to work around this issue?
Thanks!
Answer by dmart331 · Apr 09, 2018 at 07:51 PM
Hey! Late to the party here but OnMouseDrag() totally works on mobile. Here is what worked for me:
 void OnMouseDrag()
     {
         Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
         Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
 
         transform.position = objPosition;
     }
 
              For this to work, make sure you have a collider attached to your sprite.
It (On$$anonymous$$ouseDrag) works but it is not recommended on mobile devices for performance reasons.
Hi! I know it's an old post but I give it a try. I tried this drag and drop script with smartphone, and my problem is it just put the sprite in the middle of the screen, doesn't matter wheter I click with mouse or touch my smarphone screen. Anyone with the same issue? If somebody know a solution please tell me.
Your answer