Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Wolfie · Nov 13, 2018 at 05:45 PM · movementinputmousedelta

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wolfie · Nov 14, 2018 at 12:55 AM 0
Share

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.

avatar image Eno-Khaon Wolfie · Nov 14, 2018 at 10:43 PM 1
Share

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.

avatar image Wolfie Eno-Khaon · Nov 14, 2018 at 11:45 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

166 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges