I’m having a weird issue where everything works fine at 4:3, but at another resolution (ex. 16:9), ScreenToWorldPoint appears to be returning an inaccurate value.
At 16:9, clicking at the camera’s center (x = 5, y = 3) returns the correct result, but the further you get from the center in the x direction (y is always correct), the more inaccurate the x value gets. For example, when I’m expecting an x value of 1, I get 2.
void OnMouseDown () {
Vector2 pos = GetGridPosition(Input.mousePosition);
GameObject defender = Button.selectedDefender;
if (defender) {
GameObject spawned = Instantiate (defender, pos, Quaternion.identity) as GameObject;
spawned.transform.parent = parent.transform;
}
}
// Returns the grid position in whole numbers (x, y)
Vector2 GetGridPosition (Vector3 input) {
Vector2 worldPos = camera.ScreenToWorldPoint(input);
return new Vector2 (Mathf.RoundToInt(worldPos.x), Mathf.RoundToInt(worldPos.y));
}
I know the code is a little different from the lesson, but copying the code exactly does not change the result.