- Home /
Keypress interferes with mouse hover
Hello,
I have a script that requires the player to hover the mouse over an object and hold a key down (z). I set it up so the GUI displays some information OnMouseEnter, and clears that information OnMouseExit. However, it seems that holding down z rapidly activates and deactivates the mouse cursor, so that it switches between Enter and Exit, causing the GUI to flicker. Does anyone know how to make held down keys leave the mouse alone?
Thanks for taking a look at this.
EDIT: Here's an update with some script: Here's what's on the object to display and clear the GUI data:
function OnMouseExit () {
guidata.tipname = null;
guidata.tiphits = 0;
guidata.tiphead = null;
guidata.tiptorso = null;
guidata.tiplegs = null;
}
<p>function OnMouseOver () {</p>
var unitdata = gameObject.GetComponent(state);
guidata.tipname = unitdata.unitid;
guidata.tiphits = unitdata.hits;
guidata.tiphead = unitdata.head;
guidata.tiptorso = unitdata.torso;
guidata.tiplegs = unitdata.legs;
}
Hmm, the thing killed my formatting, but I think you can tell what's going on.
And here's the keypress:
function Update () {
if (Input.GetButton("z")){
gameObject.renderer.enabled = true;
}
if (!Input.GetButton("z")) {
gameObject.renderer.enabled = false;
}
}
The formatting was killed because you used the 'blockquote' button rather than the 'code' button (next to it - 1's and 0's).
Answer by · Oct 01, 2010 at 02:50 AM
I haven't tested with your code, so I can't see the described problem, but there are a few things I noticed:
You should probably do the GetComponent once, in Start(), rather than every time you release the mouse.
You should probably rewrite your Update keypress script to use GetButtonUp/GetButtonDown.
For example:
function Update ()
{
if ( Input.GetButtonDown("z"))
{
gameObject.renderer.enabled = true;
}
else if ( Input.GetButtonUp("z"))
{
gameObject.renderer.enabled = false;
}
}
Edited to remove OnMouseUp/OnMouseExit references (wrong code was pasted into question).
Whoops! I did paste the wrong code. How embarrassing. Fixed. I don't believe it's a coding problem, however, I think it's the way Unity processes inputs. And I don't know how flexible that is... Thanks!
No worries. I'm not sure of a possible cause - just posting a few suggestions that might help your code overall, even if it doesn't fix the described problem!
I appreciate it. I did move those dang variables up and have them grab the component once. I'll keep fiddling with this.
I'm home in a few hours, and will try to remember to use your code to reproduce the issue (if no-one else has solved it by then).
Your answer
Follow this Question
Related Questions
How to detect number of mouse/trackpad buttons? 0 Answers
Simulate a mouse Click? 2 Answers
Can't interact with UI when using a virtual mouse, 0 Answers
OnMouse menu 1 Answer