- Home /
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
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.
Your answer
Follow this Question
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