Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 falantar14 · Oct 30, 2013 at 03:34 PM · mobiletouchvuforiadrag

Drag object relative to camera

I'm using the Vuforia AR library and I'm trying to let the user drag the object across the screen with their finger. I've tried all sorts of different methods but everything I've found only drags the object along the axis defined by the object and not the camera.

I've tried using Camera.main.tranform.forward which I think is the key, but I haven't been able to implement it properly. Can someone kindly point me in the right direction?

Edit: This is the code I ended up using thanks to robertbu

[SOLVED]

 using UnityEngine;
 using System.Collections;
  
 public class DragObject : MonoBehaviour 
 {
     private float maxPickingDistance = 10000;// increase if needed, depending on your scene size
     
     private Vector3 startPos;
                  
     private Transform pickedObject = null;
      
     // Update is called once per frame
     void Update () 
     {
         foreach (Touch touch in Input.touches) 
         {
             //Create horizontal plane
             Plane horPlane = new Plane(Vector3.up, Vector3.zero);
             
             //Gets the ray at position where the screen is touched
             Ray ray = Camera.main.ScreenPointToRay(touch.position);
              
             if (touch.phase == TouchPhase.Began) 
             {
                 RaycastHit hit = new RaycastHit();
                 if (Physics.Raycast(ray, out hit, maxPickingDistance)) 
                 { 
                     pickedObject = hit.transform;
                     startPos = touch.position;
                 } 
                 else
                 {
                     pickedObject = null;
                 }
             } 
             else if (touch.phase == TouchPhase.Moved) 
             {
                 if (pickedObject != null) 
                 {
                     float distance1 = 0f;
                     if (horPlane.Raycast(ray, out distance1))
                     {
                         pickedObject.transform.position = ray.GetPoint(distance1);
                     }
                 }
             } 
             else if (touch.phase == TouchPhase.Ended) 
             {
                 pickedObject = null;
             }
         }
     }
 }
Comment
Add comment · Show 4
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 _milla_ · Jul 10, 2014 at 02:34 PM 0
Share

Hi there,

falantar14's code is exactly what i am looking for. The only thing i would like to change is, that the object is not snapping to the center when i touch it.

I just can't figure out how to fix the snapping to the center of the object when i start dragging. I found several Q/A in this forum where an offset is mentioned but when i try to implement any of those ideas in the code the debugger throws exceptions. Do you have any ideas how to solve my problem?

Thanks in advance!

avatar image robertbu · Jul 10, 2014 at 02:38 PM 0
Share

Post a new question with your code. To solve this problem you need to save and use the offset between where the mouse went down and the center of the object you are dragging. For example, you see a v3Offset in the code in my answer.

avatar image _milla_ · Jul 10, 2014 at 03:08 PM 0
Share

alright - thanks for your reply!!!

avatar image IamHansHarvey · Apr 09, 2017 at 02:46 PM 0
Share

how can i drag and snap an object by using touch and build in android application ? can i have some helpfull codes hehe :D

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by robertbu · Oct 30, 2013 at 03:57 PM

One solution is to use Unity's mathematical Plane class. You construct the plane using the Camera's forward and then use Plane.Raycast() to find the point on the plane. You should be able to borrow code from this answer (though you will need to make a few changes):

http://answers.unity3d.com/questions/486097/mouse-clicking-on-the-plane-and-drag-to-other-posi.html

Comment
Add comment · Show 4 · 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 falantar14 · Oct 30, 2013 at 03:59 PM 0
Share

Thanks, I will look into it and accept your answer if it works.

avatar image robertbu · Oct 30, 2013 at 04:16 PM 0
Share

Here is some sample code that implements a drag and drop relative to the camera. Attach it to the object(s) you want to drag:

 using UnityEngine;
 using System.Collections;
  
 public class Drag: $$anonymous$$onoBehaviour {
     private float dist;
     private Vector3 v3Offset;
     private Plane plane;
     
     void On$$anonymous$$ouseDown() {
         plane.SetNormalAndPosition(Camera.main.transform.forward, transform.position);
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         float dist;
         plane.Raycast (ray, out dist);
         v3Offset = transform.position - ray.GetPoint (dist);         
     }
     
     void On$$anonymous$$ouseDrag() {
          Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
          float dist;
          plane.Raycast (ray, out dist);
          Vector3 v3Pos = ray.GetPoint (dist);
          transform.position = v3Pos + v3Offset;    
     }
 }
avatar image falantar14 · Oct 30, 2013 at 04:25 PM 0
Share

Thanks I was actually able to tweak the code from your original answer to suit my needs. I'll post it here.

avatar image itsmealex100 · Mar 05, 2015 at 05:15 PM 0
Share

For anyone who needs this for 2 vectors (2D), it can be easily modified. As follows:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(BoxCollider2D))]
 
 public class Drag : $$anonymous$$onoBehaviour {
     private Vector2 screenPoint;
     private Vector2 offset;
     
     void On$$anonymous$$ouseDown() {
         
         offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
     }
     
     void On$$anonymous$$ouseDrag()
     {
         Vector2 curScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
         Vector2 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
         transform.position = curPosition;
     }
     
 }

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

18 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to rotate an object using touch controls 3 Answers

Dragging UI Image by touch 3 Answers

How Can I Drag the Object With Touch ? (Mobile) 4 Answers

Box Drag and throw away script for mobile? 0 Answers

unity2D touch drag 0 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