x


Two colliders brought into OnTriggerEnter

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);
}

What can I do to stop this?

more ▼

asked Aug 16 '10 at 01:10 AM

joedrigon gravatar image

joedrigon
231 5 6 13

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Aug 16 '10 at 01:26 AM

Marowi gravatar image

Marowi
4.9k 4 14 53

Thanks dude! Sometimes I just start confusing myself, and make things harder than they should be... lol

Aug 16 '10 at 02:31 AM joedrigon
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x293

asked: Aug 16 '10 at 01:10 AM

Seen: 986 times

Last Updated: Aug 16 '10 at 01:10 AM