Hi,
I’m working on a drag & drop script which functions like this:
mousebutton down on an object selects an object
holding mousebutton down, moves it
releasing mousebutton drops it.
Now I’m trying to make it so that the object will only drop at dropped position if it is dropped on a specific object, and will return to it’s original position if it is not. I am able to do either, but not both within the same code.
Anyone see what I’m doing wrong?
objects to dragged are tagged as “bomb” and objects they can be dragged to are tagged “grid”.
void Update () {
if (Input.GetMouseButtonDown(0) && target == null)
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,out hit,1000))
{
Debug.Log("hit tag: "+hit.transform.tag);
if (hit.transform.tag == "bomb")
{
originalBombPos = hit.transform.position;
target = hit.transform;
hit.transform.GetComponent<SphereCollider>().enabled = false;
}
}
}
else if (Input.GetMouseButtonUp(0) && target != null)
{
if (Physics.Raycast(ray,out look,1000))
{
Debug.Log("look tag: "+look.transform.tag);
if (look.transform.tag != "grid")
{
//return the bomb to it's original position
hit.transform.position = originalBombPos;
}
}
//else drop it at current position
target = null;
hit.transform.GetComponent<SphereCollider>().enabled = true;
}
if (target != null)
{
Vector3 moved = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f);
Vector3 realWorldPos = Camera.main.ScreenToWorldPoint(moved);
hit.transform.position = realWorldPos;
}
}