How can I move an object (a gameobject fill color using GUITexture for example) by mouse click/touch to grid and check contains fill color for each tile?
If use Camera.main.ScreenToWorldPoint then can't check contains. Current my code to check contains work fine but gameobject not move with mouse :(
Link image: https://i.stack.imgur.com/f6gt6.png
My code is follow:
void OnMouseDown()
{
Vector2 mousePos = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
transform.position = mousePos;
Debug.Log("OnMouseDown: transform.position: " + transform.position);
}
void OnMouseDrag()
{
Vector2 mousePos = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
transform.position = mousePos;
Debug.Log("transform.position: " + transform.position);
foreach (var x in GameBoard.Instance.listTileInGrid)
{
if (x.RectTile.Contains(mousePos))
{
Debug.Log("change color");
x.ColorId = 1;
}
}
}
Thanks in advance!
update:
i change:
transform.position = mousePos;
to
transform.guiTexture.transform.position = Camera.main.ScreenToViewportPoint(Input.mousePosition);