Hi, I’m new to Unity and C#. I am currently making an fps game in Unity 3d, and I would like to add simple building placement. Like, you can build a fence, and it will stop enemies and yourself from walking through it (and maybe degrade over time).
If anyone knows, can you please tell me? I have checked youtube for a tutorial, but I can’t seem to find one.
This is one of those questions that dips into many topics, but each of which is rather simple. There is no “just do this - done!” reply i can give you. But here are the topics to look into:
- Create prefabs for the objects you want to be placeable. If the objects should decay / despawn after a while or have some other features, do include scripts for handling this behavior.
- Provide the user a visual interface for chosing the object they want to place. So for example when pressing some button, a Minecraft-like inventory pops up at the botton, where they can chose from those prefabs. Each prefab could have an icon for that. If the game is not too fast paced, you can also select the name from a list… how you do it is up to you.
- When he choses the object, remember the choice, cast a ray through the screen and when the user presses a button (for example leftclick), place the remembered object at the location where the raycast hits the ground.
Of course the specific implementation depends on your game and your needs. The above are the basics for what i feel necessary. Of course there are a lot of conveniences to add, if you like. For example you could give the player a preview of the object he is about to place, and let him rotate it and so on before placing it. But those convenience features are a bit more advanced and you should focus on getting the basics done before.
Thanks so much for the help!