Search code examples
c#unity-game-enginegameobject

Matching objects in a drag and drop game


I am developing an educational game where I give the user a selection of words. The objective is to take these words and arrange them into a sentence by dragging each word individually and placing it into order on the provided area. I have an plane ready for each word to be dragged onto, but I am unsure how to determine whether the user has dragged their word into the correct position within the sentence.

I thought maybe I would be able to give an ID/Tag to each word, and a relating ID/Tag to each plane. When the two collide I could compare the two IDs and if they match, consider it a successful placement.

I'm wondering if anyone else has a better solution, as I am not sure about the best way to go about this?


Solution

  • Your idea is good, I don't see why not. Depending on how you implement this you can have much work if you have many words, right?

    I suggest you this: make a prefab that consists of 2 colliders (and their respective GameObjects) and one script. One collider for the word and one colider of the correct position. The script would read from somewhere (a resource xml, field in the editor, etc.) and apply the initial position for the word and the position for the 'correct position' collider. This script would also read other information (like the actual word) and make all configuration of these objets.

    This way you could easily drag 'word prefabs' to your scene and configure them individually.

    Additionally, instead of dragging this prefab, you could have an external script in your scene that would represend that 'level' (if that concept applies to your game...). The idea here is that this script could load the prefabs for that 'level' during runtime. It could even pass all the data for the prefab script to configure the objects, like I said before.

    I forgot to say the most important difference: in this method you don't have to worry about ID's. They belong to the same GameObject parent, so you can easily retrieve the objects you want in the script.