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 Cranke · Aug 23, 2015 at 08:21 PM · uiscreenmouseposition

Input.mousePosition.x is greater than Screen.width ... huh? Anyone know why?

Hi Folks,

Here is a summary of my issue:

  • I am trying to scroll a 2D image when the mouse cursor is near the right edge of the screen.

  • This is malfunctioning because the value I get from Input.mousePosition.x can be greater than the Screen.width value.

  • This causes the scrolling to happen too early (e.g. scrolling starts ~200 pixels from the right edge of the screen).

Here is a sample of my scrolling code:

 void Update () 
 {
   handleMapScroll();        
 } // End method Update  

 private int nScreenEdgeZone_px = 2;

 void handleMapScroll()
 {
     if (Input.mousePosition.x >= (Screen.width - nScreenEdgeZone_px))
     {
       print("Mouse X: " + Input.mousePosition.x);
       print("Edge  X: " + (Screen.width - nScreenEdgeZone_px));
       scrollMap(Vector3.left * nMoveSpeedHoriz_px);
     }
   [snip]
 } // End method handleMapScroll

Here is an example of the output of the print statements above. When the mouse is on the right edge of the screen, these are the values (note: monitor native resolution is 1080p and application is being run at 640x480):

  • Mouse X: 851

  • Edge X: 638

You can see that the mouse is over 200 pixels beyond the right edge of the screen (and yet, visually, it is right on the edge of the screen).

Can anyone help explain the discrepancy and what I can do about it?

Here are some other notes from my investigations:

  • On my second 1080p monitor (which is not widescreen), it seems to work perfectly.

  • My original 1080p monitor (which is widescreen) doesn't work. And, it doesn't work even if I disable all the multi-display functionality in windows. I.e. Running with only a single monitor, no duplication or extension of desktops.

  • Things always work correctly in the Unity editor no matter the resolution.

  • Things work correctly on the original monitor if I run the application in windowed mode.

  • I swear this used to work fine in the past. I've since upgraded to Unity 5 and Windows 10 ... not sure if that is relevant or coincidence.

  • If I run the application in 1080p resolution, the original monitor works fine. It only seems to have mouse position problems on non-1080p resolutions.

Any help would be appreciated.

Thanks

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
0

Answer by Eudaimonium · Sep 06, 2015 at 11:07 PM

I assume Input.mousePosition is entire display's position, while Screen actually gives you the Window of the game, not your actual computer's screen values.

Regardless, you could probably provide a more foolproof way of solving this via interactable panels.

On your UI, make a new Panel object and place it so, that when hovered over, triggers a scrolling action. Then, make a script and attach it to that panel. Within the script, you need to add additional Using statement:

 using UnityEngine.EventSystems;

Then, in your script class, implement additional interfaces:

 public class yourPanelScrollScript : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
 {
 ---


Use Right Click -> Implement Interface.

Use a public variable, something like public bool isHoveredOver = false;

Next, in OnPointerEnter event handler function, set the hovered bool to true. In OnPointerExit, set it to false.

Then, in your Update function,

 void Update()
 {
    if (isHoveredOver) linkedGameObject.DoScrolling();
 }


Where linkedGameObject is a reference to a Script object which has your scrolling logic. Assign the game object which has a scrolling-related script via the Inspector. Simply call the scrolling function, which moves the content or what ever else you need.

Comment
Add comment · 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

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

27 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

Related Questions

UNFIT SCREEN SIZE FOR MOBILE 0 Answers

UI RectTransform Position && Screen Resolution 2 Answers

Making objects be at size and place for every phone size 2 Answers

Coincide Input.mousePosition with UI element position to be on same coordinate system 0 Answers

OnPointerEnter and OnPointerExit not being triggered 0 Answers


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