- Home /
Crosshair disappears?
I have a script for my crosshair texture to follow my mouse. And when I build my game it works. But the problem is, after I hit space without moving my mouse the crosshair disappears. After I move the cursor around a bit it re-appears. How can I get it to show without disappearing?
SCRIPT:
var crosshairTexture : Texture2D;
function Start() { Screen.showCursor = false; }
function OnGUI () { var mousePos = Event.current.mousePosition; GUI.DrawTexture( Rect( mousePos.x - (crosshairTexture.width/2), mousePos.y - (crosshairTexture.height/2), crosshairTexture.width, crosshairTexture.height), crosshairTexture); }
Do you have any other code that looks for keyboard input. Looking at this code it seems like everything should be fine, which makes me think the problem is somewhere else in your code.
After taking a closer look. The crosshair doesn't disappear. But it moves to (0,0). I looked at all my scripts. None should do that. Any other reasons that this might happen?
Answer by 5parrowhawk_legacy · Jan 18, 2011 at 04:48 AM
Wild guess: hitting a key might fire an event with a null mouse position, which causes your cursor to be drawn offscreen or not at all. Try using Input.mousePosition instead of Event.current.mousePosition and see if that helps. (IIRC you may have to reverse the Y axis, but I could be wrong on that one.)
what would the code for nulling the mouse position look like?
Huh? No, what I'm saying is that hitting a key might fire an event which has a mouse position of 0,0. So Event.current.mousePosition would be 0,0, and your cursor is set to position 0,0 until a mouse event fires (which would reset the cursor). I haven't tested this so I can't confirm it, but it sure sounds like that's what's happening. Have you tried replacing Event.current.mousePosition with Input.mousePosition yet?
Answer by JoeStrout · Jan 16, 2011 at 03:03 PM
I realize this isn't much help, but it is at least a data point: I tried your script on my machine, and the problem isn't occurring here. I'm using Unity 3.1.0f4 on Mac OS X 10.6.6.