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!

Hmm... I admit, I didn't expect that still not to work. The issue sounds like having the world, instead of the local, coordinate system. But with the GameObject being part of the Projectile (or AddRelativeForce) I would expect it to be the correct one. Currently the only things I can think of, are having the wrong rb or calling AddForce only once, which both seem unlikely due to your description. Other than that it would be interesting to know what happens, if you initially rotate the projectile from sceneview and what the velocity and angular velocity are actually doing on runtime.

2 Answers

2

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.

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 "isKinematic" so that the cube gets registered with the physics system on creation.

Hey, I remembered something relating to my rb. Check my update

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.