- Home /
How to have the Player place GameObjects in scene at runtime?
Long story short, what are the concepts to let the player place GameObjects in the scene in game.
e.g. Player has built a fence section and I would like him to place it in the scene wherever they please.
I was thinking of RayCasting and then having code to change the position and rotation of the Object in question to that of the point where the ray contacts the terrain or similar. Is this the correct way to go about it? Are there other methods I should use? Is this even feasible in Unity?
My goal is for extreme customization to the environment that would mean letting the player place GameObjects wherever they deem fit.
why the eff is this not an asset? I've paid 50 dollars for assets in the past and would pay upward to 100 for a complete 'place object' system...
Answer by Danmietz · Feb 15, 2013 at 01:03 AM
GameObject.Instantiate() will let you instantiate a new instance of an existing object or prefab, with potential paramaters of a position and rotation. From the sounds of it, this is what you're after; as you suggested, the best way to go about it would be to raycast to find their selected location and then instantiate the selected object.
From there, (depending on what you wanted to do with it) you could allow the rotation/further translation of the newly instantiated object.
If you're concerned about the performance impact of instantiating objects, you could then look at pooling your objects, but as far as the functionality you're after you should be ok with the above.
That was what I was thinking. I would check for Vector3.Up and Terrain tag, yadda yadda yadda. I was just worried that this would not work for some reason, as I'm still relatively new to Unity, and program$$anonymous$$g in general so I guess I was just looking for the best way(s) to go about this.
$$anonymous$$y other idea was a grid system of some sort, kind of like what you would find in Starcraft, but I don't think that would work as well with a large 3D environment, where everything won't be lined up on the same axes.
I'm just worried my code will be convoluted and disorganized by the time I'm done, lol.
Thanks again, much obliged.
Answer by jumisko · Sep 17, 2020 at 10:50 PM
For anyone still seeking answers to this question, check out this video. It helped me a lot.