- Home /
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;
}
}
}
}
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.
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.
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 :-)
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
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.
Thats fine. Ask many questions as you can if your learning from it.