Hi!I’m new to JavaScript and i can’t figure out any way of making my mouse to stick in a specific place of the screen.I tried Screen.lockCursor but it locks in the middle of the screen.I need to give a vector2 position like (505,550).
Please help me ![]()
![]()
I’m not sure you can lock the mouse anywhere but the center. Most likely you’d have to create a texture to use as the mouse pointer (could even look identical to the current mouse icon). Lock the real mouse to the screen, then move your drawn mouse pointer based on the reported mouse movement. Respond to clicks from the drawn mouse position, not the real mouse position. The fake mouse you’re drawing could then be locked or moved in any way you wanted.
Do you have any example code,please?
You must use that to retrieve mouse movements, and add/substract them to the position of a GUI element, on the screen.
But can’t i just lock my mouse to an gui texture?
If you did that, when you lockCursor, what do you do, after, heh ?
You will have to use Physics.Raycast from the fake cursor, instead of OnMouseDown, for game objects, since the true cursor, which is locked and hidden, is always centered.
For GUI elements, you will need that instead : Unity - Scripting API: Rect.Contains
My goal is to make a crosshair for a gun,that stays locked at a position i want and if you point it to an enemy it turns red.THAT’S ALL I WANT TO DO
:x
![]()
well, i guess “You will have to use Physics.Raycast from the fake cursor, instead of OnMouseDown, for game objects, since the true cursor, which is locked and hidden, is always centered” answers my question,okay,thanks,i’ll try and i will post the script if i manage to do something ![]()
okay,so this is what i done so far and it works but the last thing i need to know is how to swap the crossharis???So when “if (Physics.Raycast (transform.position, -Vector3.up, hit, 10.0))” the white crosshair swich with a red one?
var cursorImage : Texture2D;
var teleportTarget : Transform;
function Update()
{
var hit : RaycastHit;
Screen.showCursor = false;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 10.0)) {
distanceToGround = hit.distance;
//How to swich crosshairs???
}
}
function OnGUI()
{
GUI.DrawTexture(new Rect(Screen.width /2, Screen.height / 2 , 32, 32), cursorImage);
}
Add two variables :
var redCrosshair : Texture2D;
var whiteCrosshair : Texture2D;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 10.0)) {
distanceToGround = hit.distance;
cursorImage = redCrosshair;
}
else
{
cursorImage = whiteCrosshair;
}
okay,thank you,crosshair swap works perfectly but i got another problem yet :S
if (Physics.Raycast (transform.position,Vector3.forward, hit, 30.0))
I can’t give it a correct direction because my camera rotates(i added to it a “Mouse Look” script).And looks like when i say Vector3.forward it means (0,0,1) World axis not local :S how can i change that?
I think this is the last problem
Infact i don’t even know the reason but the ideea is that i attached that script to Main Camera and i want when the Pointer has something infront turn red,not if the camera has something infront
Some pictures:It is supposed to be Circle when collides,Square when not
Very wied >.> :? :? :? :shock: :shock:
Okay,so,ehm
YAAAAAAAAAAAAAAAAAY
:o :o :o :o :o :o
Thank you so much,AkilaeTribe!
Finaly it works!I figured out why it didn’t worked…
This is the full script:
var cursorImage : Texture2D;
var teleportTarget : Transform;
var redCrosshair : Texture2D;
var whiteCrosshair : Texture2D;
function Update()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection (Vector3.forward);
Screen.showCursor = true;
if (Physics.Raycast (transform.position,fwd, hit, 30.0)) {
distanceToGround = hit.distance;
cursorImage = redCrosshair;
}
else
{
cursorImage = whiteCrosshair;
}
}
function OnGUI()
{
GUI.DrawTexture(new Rect(Screen.width / 2, Screen.height / 2 , 32, 32), cursorImage);
}
Good job ! ![]()


