Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by rudisky · Aug 01, 2016 at 02:57 PM · touch

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
6

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:

Touch

Touch.phase

Transform

Camera.ScreenToWorldPoint

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


Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rudisky · Aug 01, 2016 at 09:09 PM 0
Share

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

}

avatar image nik_u · Apr 02, 2018 at 07:37 PM 0
Share

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?

avatar image Spookytooth3D · Aug 26, 2019 at 09:43 PM 0
Share

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!

avatar image
4

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image daveztong_unity · Dec 19, 2018 at 02:21 PM 1
Share

For this to work, make sure you have a collider attached to your sprite.

avatar image Finity · Jul 08, 2019 at 05:31 PM 0
Share

It (On$$anonymous$$ouseDrag) works but it is not recommended on mobile devices for performance reasons.

avatar image gellihorvath · Aug 18, 2020 at 05:24 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sometimes, a touch can not be determined 0 Answers

GUITexture touch play animation 0 Answers

Small question about converting from Android to iOS 1 Answer

How do android touch / mouse inputs get buffered? 0 Answers

Touch Input positions are different on iOS and Android? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges