- Home /
mouse position percentage problem
in my scene I need a grid that is drawn on the screen. This grid has the pivot at the top left and every time I move the mouse it has to calculate the percentage of the size of the grid based on where the pointer is. On the Y axis it works but on the X it gives problems and the value instead of reaching 100% reaches about 60%. Can you tell me where I'm wrong? thank you
void Update()
{
Vector2 mousePos = Input.mousePosition;
mouse = Camera.main.ScreenToViewportPoint(mousePos);
if(Input.GetKeyDown(KeyCode.Mouse0))
grid = new Grid(Camera.main.ViewportToWorldPoint(mouse), gridSize);
pivot = grid.pos + grid.size / 2 - Vector2.up * grid.size.y;
Vector2 res = mouse - (Vector2)Camera.main.WorldToViewportPoint(grid.pos);
print((res / (grid.size)*100));
}
private void OnDrawGizmos()
{
Gizmos.color = Color.white;
Gizmos.DrawWireCube(pivot, grid.size);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612221444im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Snapping object to .25 grid 0 Answers
Blur script changes Viewport rect ? 0 Answers
Authoritative Rotation 2 Answers
Tile based selection deselection 1 Answer
Need Equation to flip 5x5 Matrix 0 Answers