- Home /
How to make cursor locked but active in Unity 2017?
I'm trying to have a world-space UI, and a FirstPersonController which should be able to interact with that UI via aiming reticle, basically meaning via (visible or invisible, not important to me) hardware cursor locked to the center of the screen (how Cursor.lockState = CursorLockMode.Locked does), but I still need it to dispatch events to UI elements, which CursorLockMode.Locked doesn't do
Firstly, how do I do this in any way?
Secondly, I assume all suggested answers will be more or less hacky, as all answers I found so far (which didn't work) were, so... in case a Unity employee will read this - there should be an easy and obvious way to do this, as well as a normal way to set cursor's position. To be honest, I'm very surprised there's no Cursor.SetPosition function (which would basically solve my issue as I would just be doing SetPosition(center of screen) at each frame).
OR, if I'm going about this the wrong way, what would be the right way to make Unity UI elements respond to custom-positioned reticle as if it were the cursor?
Thank you, finally someone else has the same issue! While I understand that there are certain limitation, a locked cursor should still allow clicks (and not hide the cursor), OR it should be possible to dispatch events to the UI from code. Alas, it is not so.
About positioning the cursor, if I remember correctly there is no Cursor.SetPosition()
, because it is platform dependent and it was just not possible on $$anonymous$$acOS (the cursor is controlled by the OS)., and the concept of cursor doesn't make much sense on touchscreens.
However, on Windows you can import some DLLs (I don't remember the names) that lets you set the cursor position. This is quite easy and works relatively well.
On other platforms the only possibility I can think of is that you hide the cursor ( Cursor.visible = false
) and implement your own cursor to be exactly where the invisible cursor is. To be honest, I was not able to achieve the exact positioning, but it should be possible.
"However, on Windows you can import some DLLs (I don't remember the names) that lets you set the cursor position. This is quite easy and works relatively well."
what you mean is this http://answers.unity.com/answers/1265069/view.html
which I haven't tried because to me it counts as the hackiest solution of all, even few notches above overriding the StandaloneInput$$anonymous$$odule.
"because it is platform dependent and it was just not possible on $$anonymous$$acOS (the cursor is controlled by the OS)., and the concept of cursor doesn't make much sense on touchscreens."
irrelevant. didn't stop anyone from implementing the existing cursor functionality for platforms where it makes sense ;) (i understand what you mean, and I disagree with the point which you are using this as a support argument for)
Yes, that is the answer, and I agree that it is horribly hacky, I would not have suggested it if I knew of a better solution (and probably being influenced by finding it during the development of a project where I was required to move the mouse cursor on Windows with an xbox controller :O ).
I wasn't supporting the lack of SetPosition()
, just giving the reason why (as far as I know) Unity Technologies didn't implement it. I don't see why not implement such a useful feature for platforms where it makes sense.
Anyway, let's hope something useful turns up :)
in any case, I'm happy to know I'm not alone in this :) let's see what answers we get :)
Answer by NoDumbQuestion · May 04, 2018 at 08:15 AM
@sh_code @Harinezumi You can create a custom cursor for UI mouse event when Cursor.lockState = CursorLockMode.Locked.
You could use Input.GetAxis for MouseX and Mouse Y as they represent mouse movement.
Or for advance method you could have update() method for Event.current to get real mouse delta position.
PS: Change your name sh_code. @ notification not work with your name.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162016im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
mobile VR and wireless controller conflicts 0 Answers
UI Buttons dont react on click 4 Answers
GTFO Computer terminal style interface 0 Answers
How I create game for pc and get inventory for an app on my cell phone? 0 Answers
How to make binary array toggle,How to make binary array switch/toggle? 0 Answers