- Home /
Instantiate object with button and place another object with mouse click
Hi, I'd like some help about this thing. I read a lot of tutorials and examples but couldn't find what I need. So, I'm trying to make a game object instantiate after clicking a UI Button and then wait for another mouse click to place another object. Like, the button will instantiate a small house and the second click will instantiate a bigger house. Anyone has any ideas on how to make this work? Thanks!
Do you mean click a button and a representation of the house appears and follows the pointer around and then you click on the terrain and the actual gameObject instantiates at that point?
That said I'm just going out so can't answer just yet, maybe it'll help someone else help you or I'll look later when I get back.
Answer by RussianBsack · Feb 20, 2015 at 02:07 PM
I was looking into this before and seem to work it out, are you familiar with Ray casts? That's what you need. Here's an example, it should get you stared, i figured it out last night so didn't have much time to experiment and modify the code but it'll do what you need.
void Update()
{
ghost();
}
void ghost () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit))
{
transform.position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
}
}
}
Attach that piece of code to the item that you want to 'mimic'. Make sure that its in the scene too.
Answer by Mmmpies · Feb 21, 2015 at 02:21 PM
Just answered a similar question for someone else, you might get more info from the script in that answer:
what it does is click on a 4.6 UI button which instantiates an Image of a building that follows the mouse position and when the terrain is clicked it then instantiates that building at the terrain with a Raycast.
You could easily replace the Image with a smaller 3D copy of the house.