- Home /
Matching objects using drag and drop
I am currently making a game where you have to match a term to a specific location on a field. The player will drag and drop, via mouse click, a term to the correct location on the field. I was thinking of doing a tag match between the object that will be the term and the plane that will be the location. Something like,
if gameObject.tag == gameObject.tag
do something (like score +1 or something)
However, I can't seem to figure out exactly how to code this. I'm new to Unity but understand c#. All in all, my question is how would I go about matching the tags of two objects?
Edit: This is the current code I have for drag/drop. It's just something I came across so nothing really set in stone.
void OnMouseDrag () {
point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
point.y = transform.position.y;
transform.position = point
Do you have your drag/drop code already? The drag (mouseDown) event could be associated to a gameObject variable and the drop would be a different variable so the compare is if(gObj1.tag = gObj.tag)...; if you have your drag/drop, share it which should make this easier to answer
Alright, duly noted. And if I understand correctly, the drop would have it's own variable separate from the drag? So I would need to rework my drag and drop code as well?
Answer by Noob_Vulcan · May 07, 2014 at 05:19 AM
if (Input.GetMouseButtonDown(1)){
Ray ray = nGUICam.ScreenPointToRay (Input.mousePosition);
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (ray, out hit)) {
if (hit.transform.CompareTag ("Your_Object_Tag")) {
pickedObject = hit.transform;
}
} else {
pickedObject = null;
}
}
Now you have your pikedObject transform .Now do stuff in
void OnMouseDrag(){
//do stuff
pickedObject.transform.position= Input.mousePosition;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Finding transform.position of Object with specific Tag 0 Answers
About scene change: is my method bad programming? 1 Answer
Matching 4 specific object question 0 Answers