- Home /
Question by
12234234234234 · Mar 14, 2013 at 02:55 AM ·
movementgameobjectmousetouchmouse-drag
Moving GameObjects with mouse and check contains
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: http://i.imgur.com/cHoS0YV.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!
Comment
I see what you are doing, but I don't understand your problem. GUITextures live in viewport space. So to get one to track the mouse, you can:
gtTrans.position = Camera.main.ScreenToViewportPoint(Input.mousePosition);
gtTrans is the transform for the GUITexture. You can leave your contains code alone.