- Home /
Question by
Deeo · Dec 14, 2012 at 07:37 AM ·
transform.positionmouseclickmousemove
Trying to move a instantiate gameobject with mousedown
I've been trying for a while to create a gameobject from a prefab with a GUI button and while holding down the button move it to its location then release the button and have the object placed there.
I do not want to use any raycast since there is no plane in the game.
So far i can get the object to spawn where i push the button but not move.
Any help on this would be greatly appreciated.
public void OnGUI()
{
bool Item Drag
if(GUI.Button (DRButton1, Door))
{
GameObject Box = (GameObject)Instantiate(Model_Box, Camera.main.ScreenToWorldPoint(Input.mousePosition), Quaternion.Euler(0,0,0));
Box.name = "Box";
ItemDrag = true
}
if(ItemDrag)
{
if(Input.GetMouseButtonDown(0))
{
Box.transform.Translate(Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
}
}
Comment
Best Answer
Answer by clunk47 · Dec 15, 2012 at 04:29 AM
GetMouseButtonDown is only one frame. If you want to do something while holding mouse button, use GetMouseButton.
public void OnGUI()
{
bool Item Drag
if(GUI.Button (DRButton1, Door))
{
GameObject Box = (GameObject)Instantiate(Model_Box, Camera.main.ScreenToWorldPoint(Input.mousePosition), Quaternion.Euler(0,0,0));
Box.name = "Box";
ItemDrag = true
}
if(ItemDrag)
{
if(Input.GetMouseButton(0))
{
Box.transform.Translate(Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
}
}