- Home /
Unity + Vuforia Drag and drop not working properly
Good Morning Everybody, I am a design student who has just started to approach to unity scripting. I am trying to drag and drop gameObject to his assign place. As I work in a normal unity scene, the script works. As long as I put it in a scene where Vuforia is set, the script works partially. What happens is that if I drag the gameObject into the range I set for make it stick into his place, it sticks, if else I drag it in a wrong place, instead of coming back to its initial position, it flies in the outer space. Furthermore, as I try to apply the script in another scene with the same settings, it does not to work in the same way and the object position has an offset compared to the mouse position.
here is the script,
using System.Collections; using System.Collections.Generic; using UnityEngine; using Vuforia;
public class try2 : MonoBehaviour {
public static bool locked;
public GameObject NotePlace;
public GameObject Note;
private Vector3 initialPosition;
void Start()
{
GameObject NotePlace = GameObject.Find("RePlace");
GameObject Note = GameObject.Find("Re");
Note.transform.position = initialPosition;
}
void OnMouseDrag()
{
Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, -Camera.main.transform.position.z + Note.transform.position.z);
Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
Note.transform.position = objPosition;
}
void OnMouseUp()
{
if (Mathf.Abs(transform.position.x - NotePlace.transform.position.x) < 0.3f &&
Mathf.Abs(transform.position.y - NotePlace.transform.position.y) < 0.3f)
{
Note.transform.position = new Vector3(NotePlace.transform.position.x, NotePlace.transform.position.y, NotePlace.transform.position.z);
locked = true;
}
else
{
Note.transform.position = new Vector3(initialPosition.x, initialPosition.y, initialPosition.z);
locked = false;
}
}
}
Your answer
Follow this Question
Related Questions
Cloud recognition in Vuforia 0 Answers
Using Collision to trigger animation in AR environment using Vuforia 0 Answers
Drag object not working in Vuforia ? 1 Answer
Drag object relative to camera 1 Answer
How to make a moveable lever? (2D) 0 Answers