- Home /
Placing static prefabs on click
I'm not sure how i would describe this, so i'm just going to describe what i'm aiming for.
So far, i have a character that runs through a field of cubes. I want almost a permanent level editor effect, wherein the character can click on a cube to place another cube on the top, side, or bottom of it. (Depending on where on the first cube they click.) So you can edit the landscape and create structures by placing these cubes. (Like a building game)
Would it be a matter of instantiating cube prefabs on a click event? I'm a bit of a newbie to Unity!
Answer by Waz · Aug 07, 2011 at 10:11 PM
Yes, you want the Instantiate function. The click event can be processed in an OnGUI function, Update upon Input.GetButtonDown, or using an OnMouseDown function.
Yes, but how would i get the instantiated cube to remain static on the clicked face?
If it doesn't have a Rigidbody, why would it not? If the other cubes are moving, you would need to parent the new cube to the mother cube (i.e. set transform.parent
).
It's much better for it to have a Rigidbody that is set "is$$anonymous$$inematic" so that the cube gets registered with the physics system on creation.
Answer by sven1994 · Aug 08, 2011 at 01:24 AM
I think when instantiating a lot of these cubes you will run into performance issues very quickly. What you are aiming sounds like a minecraft style game. Sooner or later you will have to create one Mesh for lots of cubes instead of one for a single cube. But this has been discussed a few times already, just use the search bar at the top.
Your answer
Follow this Question
Related Questions
How to instantiate a custom sized object? 1 Answer
Instantiating Cube Prefabs with scale 3 Answers
How to instantiate a prefab at mouse pos 1 Answer
Adding Imperfections? 1 Answer