- Home /
GetKey equivalent for mouse movement
If I wanted to use the Unity Input Manager to register a keyboard input I'd conventionally use GetButtonDown, however as part of a self imposed learning project I'm shunning much of Unity's perfectly good input manager version in favour of writing my own (almost certainly inferior) custom one. As such I'm now using GetKeyDown instead and referencing ones from my own manager which stores each input as a KeyCode. When it comes to mouse button pressing this also works because mouse0 and mouse1 are both available as KeyCodes, but mouse movement isn't. What kind of equivalent is there that could allow me to obtain raw mouse delta values without requiring the Unity Input Manager's axis system?
Answer by Eno-Khaon · Nov 13, 2018 at 07:29 PM
There are numerous control devices supported in Unity's Input class. Be sure to take a look at their documentation and see what options are available to you.
To address your question directly, mouse input is primarily detected using Input.GetMouseButton(int) (and its Up and Down variants), Input.mousePosition, and input.mouseScrollDelta for the mouse wheel.
Furthermore, to get the per-frame mouse position change (i.e. mouseDelta), you can try something like this:
void Update()
{
mouseDelta = Input.mousePosition - lastFrameMousePosition;
lastFrameMousePosition = Input.mousePosition;
}
Thanks for the response! I have to admit that I'm still a bit confused though. When using Input.mousePosition to obtain a mouse delta, this falls apart when the mouse hits the edge of the screen (or window, depending on Cursor.lockState) because it cannot continue further on one or both axes and the delta will become zero in spite of further mouse movement. I thought I could solve this by resetting the mouse position to the screen center after recording the last frame position, but even this hack this isn't a viable solution because Input.mousePosition is readonly. I'm also unable to use Cursor.lockState to keep it centralised, as this means mousePosition can't change at all.
Unless there's something I've overlooked, it might not be possible to get a strict mouse delta without using either Input.GetAxis(string) or Event.delta through OnGUI() (or by using external calls yourself). As it stands, the input manager makes external calls to get information otherwise unavailable, such as joystick inputs (including analog input through GetAxis()).
I certainly hope I can be corrected on that, but I haven't been able to locate any means of acquiring raw mouse delta data without using either of those two input schemes.
Event.delta is an avenue I hadn't explored previously, but sadly it turns out it behaves the same way as the earlier Input.mousePosition comparison when it hits the screen edge, only using Event.mousePosition ins$$anonymous$$d, but with the slight added awkwardness of being a GUI event. I must admit it does look like you're right and that we're limited to the Unity Input $$anonymous$$anager to handle these things, but if it's O$$anonymous$$ by you I'd like to leave the question up a bit longer before marking your response as correct, mostly because it only takes one example to prove the existence of something, as opposed to the impossible task of proving something's nonexistence I guess, so maybe I'll get lucky. Thanks again for your input (pun intended).
Your answer
Follow this Question
Related Questions
How to Move a Cube 3 Answers
Moving a gameobject with mouse 1 Answer
How to implement "Homeworld style" movement orders with Unity? 1 Answer
Mouse movement swings character around in circles 1 Answer
Move to mouse position 1 Answer