- Home /
Question by
JohnArthur · Jul 24, 2014 at 02:34 AM ·
gameobjectmouseposition
Selecting and Placing objects from GUI
What I want to happen, is this:
I click on a GUI button.
A gameObject instantiates and is following the mouse Position.
I move my mouse around and the gameObject follows or is attached to my mouse.
When I want to place the gameObject, I click and the gameObject is there and no longer moved by the mouse.
My Code so far:
Ray ray = currentCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Vector3 mousePos = currentCamera.ScreenToViewportPoint(Input.mousePosition);
Vector2 mousePos2 = currentCamera.ScreenToWorldPoint(Input.mousePosition);
Vector3? newlyPlacedItem = null;
if(GUI.Button(new Rect(Screen.width - 500, Screen.height - 70,70,50),"First Item"))
{
Debug.Log("clicked");
placeThis = prefab_firstItem;
nowPlace = true;
if(nowPlace == true)
{
Debug.Log (nowPlace);
Instantiate(placeThis,mousePos2,Quaternion.identity);
placeThis.transform.position = mousePos2;
if(Input.GetMouseButtonDown(0))
{
Debug.Log ("itemPlaced");
nowPlace = false;
}
}
Comment
Answer by DontShooot · Jul 24, 2014 at 05:32 AM
I think GUI.Button returns true once, when you clicking the object, but you have to set your object position in Update()