0
\$\begingroup\$

Suppose there are 4 areas namely A1,A2,A3,A4 and each object is given a number(1,2,3,4). When I drag and drop the object(1) on A1, it should print a message "where the object is being dropped". (no need of mouse over... any mouse collision concept) enter image description here

first set i=1 -----> o1

ie;logic

if(mousecollision == "L1" && i=1)
{
  mouseup
  score++;
}
else
{
  score--;
}
\$\endgroup\$
  • \$\begingroup\$ Do you mean "layers" or "areas"? \$\endgroup\$ – wondra Aug 4 '14 at 18:42
  • \$\begingroup\$ @wondra:areas........Once who successfully finish this game he would be able to correctly identify objects located in these ten regions.If on the other hand he is unable to place the element into its location or he incorrectly places the element that element will disappear followed by the appearance of new element. \$\endgroup\$ – Deepika C P Aug 5 '14 at 4:43
  • \$\begingroup\$ answers.unity3d.com/questions/764081/… \$\endgroup\$ – Deepika C P Aug 5 '14 at 5:03
  • \$\begingroup\$ Then it is just about comparing two numbers. \$\endgroup\$ – wondra Aug 5 '14 at 9:23
2
\$\begingroup\$

This a several-step answer and this is just a way of doing things, but certainly not the only one.

  1. Both objects and areas must have a collider component attached.
  2. Create the proper tags for each set to combine. In this case they're "Green" and "Purple".
  3. Create a layer for the areas. In this case it's "DDArea".
  4. Use the following script, attached to the D&D objects, as an example to your own. In this case I used spheres.

The train of thought goes as follows:

  • Take advantage of the MonoBehaviour functions for handling mouse collision (OnMouseDrag and OnMouseUp) for moving the objects and detecting release, respectively.
  • Move the object according to the position of the mouse.
  • Cast a ray on release in order to know whether the mouse's position (thus, the object's position) is inside an area.
  • If that's the case, then compare the area's and the object's tags (sets), so a decision can be made accordingly; reset the object to its initial position or increase the score.

Finally, here's my example running on Unity web player: https://dl.dropboxusercontent.com/u/4568142/unity/dragdrop/dragdrop.html

Inspector for the objects representing the areas

Inspector for the objects representing the areas

Inspector for the objects representing the objects (balls/spheres)

Inspector for the objects representing the objects (balls/spheres)

DragDropBall.cs:

using UnityEngine;
using System.Collections;
public class DragDropBall : MonoBehaviour
{
    public Camera mainCam;
    int layerMask;
    Vector3 initPosition;

    void Start()
    {
        if (mainCam == null)
            Debug.LogError("No camera attached");
        layerMask = 1 << LayerMask.NameToLayer("DDArea");
        initPosition = gameObject.transform.position;
    }

    void OnMouseDrag()
    {
        Vector3 newPosition = mainCam.ScreenToWorldPoint(Input.mousePosition);
        newPosition.z = transform.position.z;
        transform.position = newPosition;
    }

    void OnMouseUp()
    {      
        Vector3 origin = mainCam.ScreenToWorldPoint(Input.mousePosition);
        Ray ray = new Ray(mainCam.ScreenToWorldPoint(Input.mousePosition), Vector3.forward);
        RaycastHit[] hits = Physics.RaycastAll(ray, Mathf.Infinity, layerMask);
        foreach (RaycastHit h in hits)
        {
            string nameSphere = gameObject.name;
            string nameArea = h.collider.gameObject.name;
            string tagSphere = gameObject.tag;
            string tagArea = h.collider.gameObject.tag;
            Debug.Log(nameSphere + " dropped on " + nameArea);
            if (tagSphere.Equals(tagArea))
            {
                Debug.Log("Increment points");
                // further logic for your game here
            }
            else
            {
                // return the object to its inital position
                gameObject.transform.position = initPosition;
            }
        }
    }
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.