Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by BeHappy · Aug 06, 2012 at 09:15 AM · vuforiadrag

Drag object not working in Vuforia ?

Am working on Vuforia's "Image Target" Example. It was working quite good and now i need to drag the object(simply a cube) that appears while recognizing the image tracker. Am using the same script for the standard unity game and working fine, but not in the case of Vuforia.

Please help on this.

 var targetItem : GameObject;
 
 var horizontalLimit : float = 2.5;
 var verticalLimit : float = 2.5;
 
 private var scrollDistanceX : float;
 private var scrollDistanceY : float;
 
 private var hit: RaycastHit;
 private var layerMask = (1 <<  8) | (1 << 2);
 
 function Start()
 {
  scrollDistanceX = targetItem.transform.position.x;
  scrollDistanceY = targetItem.transform.position.y;
  layerMask =~ layerMask; 
 }
 
 function FixedUpdate()
 { 
  if (Input.touchCount > 0) 
  { // If there are touches...
  var theTouch : Touch = Input.GetTouch (0); 
  
  var ray = Camera.main.ScreenPointToRay(theTouch.position);
  
           if(Physics.Raycast(ray,hit,50,layerMask))
           { 
           if(Input.touchCount == 1)
  { 
  var scrollDeltaX = theTouch.deltaPosition.x;
  var scrollDeltaY = theTouch.deltaPosition.y;
  
  scrollDistanceX = Mathf.Clamp(scrollDistanceX+scrollDeltaX*Time.deltaTime*.5,-horizontalLimit,horizontalLimit);
  scrollDistanceY = Mathf.Clamp(scrollDistanceY+scrollDeltaY*Time.deltaTime*.5,-verticalLimit,verticalLimit);
  
  targetItem.transform.position.x = scrollDistanceX;
  targetItem.transform.position.y = scrollDistanceY;
  }
  }
  }
 }
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

1 Reply

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

Answer by $$anonymous$$ · Aug 06, 2012 at 10:16 AM

I'm not good with JavaScript so can't edit your script. But that been said, why are you doing input handing code inside a FixedUpdate(). It's not an ideal place to do it I suppose. Anyway here is what I would do if I get this kind of a problem.

Create a new script called 'MovableObject' and attach it to your object that you want to move. The script doesn't need to have anything in it like so.

 using UnityEngine;
 using System.Collections;
 
 public class MovableObject : MonoBehaviour 
 {
  
 }

Then create the input handling part like this.

 using UnityEngine;
 using System.Collections;
 
 public class InputHandler : MonoBehaviour 
 {
  private Ray m_Ray;
  private RaycastHit m_RayCastHit;
  private MovableObject m_CurrentMovableObject;
  
  void Update () 
  {
     if(Input.touches.Length == 1)
     {
     Touch touchedFinger = Input.touches[0];
  
     switch(touchedFinger.phase)
     {
     case TouchPhase.Began: 
     m_Ray = Camera.mainCamera.ScreenPointToRay(touchedFinger.position);
     if(Physics.Raycast(m_Ray.origin, m_Ray.direction, out m_RayCastHit, Mathf.Infinity))
     {
        MovableObject movableObj = m_RayCastHit.collider.gameObject.GetComponent<MovableObject>();
        if(movableObj)
        {
           m_CurrentMovableObject = movableObj;
        }
     }
     break;
     case TouchPhase.Moved:
         if(m_CurrentMovableObject)
        {
           m_CurrentMovableObject.gameObject.transform.Translate(Vector3.left * touchedFinger.deltaPosition.x);
        }
     break;
     case TouchPhase.Ended:
        m_CurrentMovableObject = null;
        break;
     default:
        break;
     }
    }
  }
 }

Obviously the object only moves in X because thats the way that I have coded it. But you can change that anytime. This does the job. Hope this answers your question.

Comment
Add comment · Show 15 · 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 $$anonymous$$ · Aug 06, 2012 at 10:25 AM 1
Share

you don't need to have the '$$anonymous$$ovableObject' script. Actually this would work with a tag as well. like 'm_RayCastHit.collider.gameObject.tag'. But this way is more extendable I think. You can call other methods in '$$anonymous$$ovableObject' if you plan to have any.

avatar image BeHappy · Aug 07, 2012 at 05:05 AM 0
Share

hdsenevi....Thanks a lot for your help, it was working well. I searched for some example scripts regarding drag and everyone was using FixedUpdate only in those examples and i followed the same.

Your's is so simple and works perfect.

Thanks a ton :-)

avatar image $$anonymous$$ · Aug 08, 2012 at 09:47 AM 1
Share

Edit the InputHandler script like so

public class InputHandler : $$anonymous$$onoBehaviour { ... private float m_$$anonymous$$ovement$$anonymous$$ultipier = 10f;

void Update () { ... case TouchPhase.$$anonymous$$oved: if(m_Current$$anonymous$$ovableObject) { m_Current$$anonymous$$ovableObject.gameObject.transform.Translate(Time.deltaTime m_$$anonymous$$ovement$$anonymous$$ultipier new Vector3(touchedFinger.deltaPosition.x, 0, touchedFinger.deltaPosition.y)); } break; } }

I didn't paste the entire script in here because it will be hard to read without the "code tag" (since this is a comment.). Anyways, here is a link to the script if you want https://www.dropbox.com/s/ptfhe7t8vkku0fs/InputHandler.cs

avatar image $$anonymous$$ · Aug 08, 2012 at 10:00 AM 1
Share

Yes, you only have to replace that line with this and it would work.

m_Current$$anonymous$$ovableObject.gameObject.transform.Translate(Time.deltaTime m_$$anonymous$$ovement$$anonymous$$ultipier new Vector3(touchedFinger.deltaPosition.x, 0, touchedFinger.deltaPosition.y));

In this instance it only moves the object in X and Z... But if you want to move the object in X and Y just change the code to this.

m_Current$$anonymous$$ovableObject.gameObject.transform.Translate(Time.deltaTime m_$$anonymous$$ovement$$anonymous$$ultipier new Vector3(touchedFinger.deltaPosition.x, touchedFinger.deltaPosition.y, 0));

It should be pretty obvious right? and if the movement speed is too slow, increase m_$$anonymous$$ovement$$anonymous$$ultipier value. And if it's moving in the opposite direction make deltaPosition.x and deltaPosition.y negative.

avatar image $$anonymous$$ · Aug 08, 2012 at 10:24 AM 1
Share

Thats fine. Ask many questions as you can if your learning from it.

Show more comments

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

13 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

Related Questions

Drag object relative to camera 1 Answer

Using Collision to trigger animation in AR environment using Vuforia 0 Answers

How to drag the 3D model in screen with finger 1 Answer

Cloud recognition in Vuforia 0 Answers

Unity + Vuforia Drag and drop not working properly 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