I have 2 blocks with the same script on each one because I think it needs to be. The problem is when I drag the one block into the other my OnTriggerEnter is run for both blocks. I understand why its happening but I need it to happen to the block that is not currently being click/dragged.
void OnMouseDown()
{
originalPosition = transform.position;
}
void OnMouseDrag()
{
// Get our current mouse position
currentPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// If we don't set z to blocks orig z it will put the block at 0
// with camera and put it out of view
currentPosition.z = transform.position.z;
// Move object each frame with mousedrag active
transform.position = currentPosition;
// Hide mouse cursor
Screen.showCursor = false;
}
void OnMouseUp()
{
// Show mouse cursor
Screen.showCursor = true;
// On mouse up move block to original position for now
transform.position = originalPosition;
}
void OnTriggerEnter(Collider blockCollider)
{
Debug.Log("We hit someting!!!" + blockCollider.name + " " + blockCollider.tag);
}
I'm not sure it's the best way, but you could just have a boolean that you toggle in OnMouseDrag and OnMouseUp, and then check that the GameObject isn't being dragged in OnTriggerEnter.
i.e.
var beingDragged : boolean = false;
void OnMouseDown()
{
originalPosition = transform.position;
}
void OnMouseDrag()
{
// Get our current mouse position
currentPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// If we don't set z to blocks orig z it will put the block at 0
// with camera and put it out of view
currentPosition.z = transform.position.z;
// Move object each frame with mousedrag active
transform.position = currentPosition;
// Hide mouse cursor
Screen.showCursor = false;
beingDragged = true;
}
void OnMouseUp()
{
beingDragged = false;
// Show mouse cursor
Screen.showCursor = true;
// On mouse up move block to original position for now
transform.position = originalPosition;
}
void OnTriggerEnter(Collider blockCollider)
{
if (!beingDragged)
{
Debug.Log("We hit something!!!" + blockCollider.name + " " + blockCollider.tag);
}
}
I had a quick search but couldn't find a simple method to find which object was currently 'selected' (i.e. the focus of a MouseDrag). I'd be interested in a better solution, but this should work.