- Home /
How to get equal screen coordinates on different resolutions?
Hello guys! Im currently making a game, where you need to outilne a symbol on screen, so easiest way i found is to store coordinates of a symbol and check if mouse is near and make calculations based on that. Everything works for me, but on diffrent resolutions in same spots mouse position is diffrent.
I came up with this script that scales positons perfectly, but i have an offset on x axis, how can i remove it?
Reference resolution i have 720x1280, if resolution stays in the same ration like 360x640 everything is perfect, but other resolutions i have x offset.
Rect canvasSize = GameObject.Find("Canvas").GetComponent<RectTransform>().rect;
Vector2 resolution = new Vector2((float) currentScreenSize.x / canvasSize.width, (float) currentScreenSize.y / canvasSize.height);
corners.Add(new Vector2(385 * resolution.x, 890 * resolution.y));
corners.Add(new Vector2(172 * resolution.x, 395 * resolution.y));
corners.Add(new Vector2(550 * resolution.x, 395 * resolution.y));
corners.Add(new Vector2(381 * resolution.x, 594 * resolution.y));
Answer by Dragate · Oct 27, 2017 at 07:11 AM
I would change that "Match" in Canvas Scaler to 0.5.
If I were in your shoes, I would not store the coordinates directly. I would normalize them first. So your corners would be in the range of [0,1] regardless of screen resolution. When selecting a symbol you would get Input.mousePosition and normalise that as well (x / Screen.width and y / Screen.height). This way you make your game independent of devices' screen resolutions.
So if im looking coordinated on screen 720x1280, an point is (385,890), i should do (385/720,890/1280) and get (0.53472 , 0.6953125), and it will be the same for resolution like 720x360 ? Or i should find coordinate in another way independent of my current resolution?
Yea, you got it. If for a corner on x axis you have 385 in a 720 screen (which is almost half) and you keep the 0.53472, then it's good. If you get another screen resolution of let's say 1440 pixels on x axis, that would place the corner on 0.53472 * 1440, which results in a pixel coordinate again almost half of the screen width.
I edited my code and canvas scaler, but something weird happens xD.
corners.Add(new Vector2((float)385 / 720 * currentScreenSize.x, (float)890 / 1280 * currentScreenSize.y));
corners.Add(new Vector2((float)172 / 720 * currentScreenSize.x, (float)395 / 1280 * currentScreenSize.y));
corners.Add(new Vector2((float)550 / 720 * currentScreenSize.x, (float)395 / 1280 * currentScreenSize.y));
corners.Add(new Vector2((float)381 /720 * currentScreenSize.x, (float)594 / 1280 * currentScreenSize.y));
As you can look from picture points transformed this way are not in positions i want xD If i use $$anonymous$$atch to 1 Height or width, they have right height or width,but not both, 0.5 is wrong all the way, maybe you know whats the problem?