x


Detecting when a game object is clicked, when the mouse is held down, or enters or leaves a GameObject

I stumbled upon this post which was pretty useful:

http://answers.unity3d.com/questions/3209/basic-on-off-movement-using-mouse-click

My dilemma isn't exactly his though. His is more toggling a button while mine is a hold.

Problem 1: The button should de-activate when onMouseUp, easy modification, but it introduces another problem:

Problem 2: The button should de-activate when the mouse leaves its area. So if the guy drags his mouse off the button, it should deactivate.

Anybody know how to program this?

Thanks in advance!

more ▼

asked Jan 29 '10 at 04:18 AM

Peter B-B gravatar image

Peter B-B
236 13 15 21

thanks it works!!! =)

Jan 09 at 05:58 PM j1c2

Asked January 29. Of last year (or, probably, earlier). :P

Jan 09 at 06:06 PM Loius
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

There's an OnMouseExit event which you can use just like OnMouseUp, to trigger code when the mouse is no longer over the object. You'd probably have to use this in conjunction with the mouse up/down tests to get the effect you want.

(note, the comments appear to be wrong in the example on the documentation page linked above)

Here's an example which activates an object when clicked, and deactivates when the mouse is released, or when the mouse exits. Notice that it also reactivates if the mouse is brought back over the object while still held down (standard behaviour for normal Mac and Windows UI buttons). I'm using a boolean variable called 'wasClicked' to remember the state, to achieve this. The Activate() and Deactivate() functions are custom functions - I have just added code which makes the object change colour as an example, but you can put whatever you want in there.

var wasClicked : boolean;

function OnMouseDown() {
    wasClicked = true;
    Activate();
}

function OnMouseUp() {
    wasClicked = false;
    Deactivate();
}

function OnMouseEnter() {
    if (wasClicked) {
        Activate();
    }
}

function OnMouseExit() {
    Deactivate();
}

function Activate() {
    renderer.material.color = Color.red;
}

function Deactivate() {
    renderer.material.color = Color.white;
}
more ▼

answered Jan 29 '10 at 12:03 PM

duck gravatar image

duck ♦♦
41k 92 148 415

Thank you so much it works!!

How would I go about doing this for an iPhone seeing as how the iPhone doesn't have the mouse functions (I think)?

Jan 29 '10 at 04:03 PM Peter B-B

@Peter I wish that comment had been answered...

Dec 03 '10 at 02:51 PM Cawas
Dec 03 '10 at 08:21 PM Cawas
(comments are locked)
10|3000 characters needed characters left

Thank you so much! Very useful.

more ▼

answered Sep 23 '12 at 09:14 PM

Vinícius MM gravatar image

Vinícius MM
-4

(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:

x5099
x2097
x986

asked: Jan 29 '10 at 04:18 AM

Seen: 23310 times

Last Updated: Jan 09 at 06:06 PM