0

I am new to unity. I am working on cake maker like game for android device. I am facing a difficulty in dragging objects from cabinet to table. On table i want to mix them in a jar. These are Many items. So, I cannot do it by separating them using unity Tags. How to make a generic way to drag them on table?

I am using this code but its not working for me.

   using UnityEngine;
   using System.Collections;

   public class DragObjects : MonoBehaviour {

    Ray ray;
    RaycastHit hit;

    // Use this for initialization
    void Start ()
    {
    }

    // Update is called once per frame
    void Update ()
    {

        if (Input.GetMouseButtonDown(0))
        {
            ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            if (Physics.Raycast (ray, out hit))
            {
                if (hit.collider.tag == "oil")
                {
                    OnMouseDrag();
                }
            }
        }

    }


void OnMouseDrag()

  {

    Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);


    point.x = gameObject.transform.position.x;

    gameObject.transform.position = point;
  }


}
4
  • Which part is not working. Does it detect your collision (try putting a print on the RayCast)? Also, I can't remember exactly but I think if you don't set the Z-value on the Input.mousePosition to some distance then the point will just be the camera position (imagine the ray coming out of the camera being length 1)
    – T. Kiley
    Feb 13, 2014 at 13:05
  • yes, it is detecting the collision. But not working to drag. I tried to check the code using Debug.Log() and also By Z-value. When i click the object it does not drag . but changing position. some time some Z-Valve when i use just (point.x = gameObject.transform.position.x;). some time in front of camera when i use both (point.x = gameObject.transform.position.x; && point.z = gameObject.transform.position.z;). Feb 13, 2014 at 14:51
  • Sorry I don't understand, is the object moving, but incorrectly or not moving at all?
    – T. Kiley
    Feb 13, 2014 at 17:09
  • Moving but incorrectly.Object is changing its position directly from one point to other. Not able to pick up the object on onclick and place it on other position(Table). Feb 14, 2014 at 5:49

1 Answer 1

1

The Unity way of doing this would be to have a Draggable behaviour which you attach to each object you wish to drag.

If you are using 2D colliders you can use something like this:

Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
bool isOver = collider2D.OverlapPoint(pos);

With 3D colliders you can get a ray from the camera and see if that intersects with a draggable.

Then you can use other colliders to detect when they are placed on the jar.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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