- Home /
Any way at all to center the cursor?
Hi, I have a game object, a flashlight, that I want the player to be able to pick up. I have a crosshair in the center of the screen. The pick up script says that I have to click on the object with the mouse to pick it up, and I thought that would be fine, since I thought that Screen.lockCursor centres the cursor AND hides it. But apparently it doesn't centre it because when I try to pick up the item, I can never pick it up while directly looking at it!
I tried looking up a way, and I seriously cannot find a working solution, sometimes it would be said it was impossible at the moment!
Is there any way to truly centre the cursor?
Or could some one advise me an alternative to my pick up script?
Pick up script (the relevant part):
function OnMouseDown () {
gameObject.active = false;
Destroy(gameObject);
}
if ( Input.Get$$anonymous$$eyUp ( "f" ) ) {
Screen.lockCursor = true;
Screen.lockCursor = false;
that works for me. class type Update.
Answer by kolban · May 07, 2012 at 07:45 PM
This is a good question but unfortunately, I don't think we will be able to achieve what you want. Control over the location of the mouse cursor is considered a very privileged operation. Imagine a malicious (or just plain broken) application could control the mouse pointer. This would mean that the mouse could simply NOT be moved out of the window rectangle. For many "simple" users of computing, this would be very intrusive. Imagine no way to get to the task bar to "Close" an application. Because of this, access to control the location of the mouse cursor is considered "constrained".
A second reason is more subtle but still very practical. On my desk, my mouse lives on my mouse pad. When the cursor is centered in the screen, the mouse happens to be in the center of my mouse pad. When I move the mouse to the left of the pad, it is on the left of the screen (and so on). Repositioning the mouse cursor would disassociate the physical location of the mouse on the mouse pad to its logical location on the screen. Again, many users simply aren't aware that they pick up the mouse and move it themselves ... but some are.
I see :(
All I want to do is just make the flash light picked up with some sort of interaction, so I could make an alternative way of 'picking up' the flash light? right? I don't want to use keys, but I will if I have to I guess.
Answer by Piflik · May 07, 2012 at 07:53 PM
You could cast a ray from the camera to the center of the screen and if it hits the flashlight (and the player is close enough) it will be picked up.
with Piflik
I had the exact same problem because I was using raycast based on the mouse's screen position. If you remove the mouse position dependency and use the center of the screen ins$$anonymous$$d it works fine.
if(RAY_FRO$$anonymous$$_CENTER){
mousePos.x = Screen.width * 0.5f;
mousePos.y = Screen.height * 0.5f;
}else{
mousePos = Input.mousePosition;
}
Ray ray = Camera.main.ScreenPointToRay(mousePos);
if (Physics.Raycast(ray, out hit, rayDistance, layer)) {
debugColor = Color.green;
collided = hit.collider;
}
Topic is old but it might help someone else :)
Your answer
Follow this Question
Related Questions
Locking cursor/mouse on an object 1 Answer
Locking the mouse cursor without it centering after unlocking it? 0 Answers
lock camera to when open popup screen 3 Answers
Cursor Lock 2 Answers
Modifying the pause menu to have screen.lockcursor on at start 1 Answer