Unity Block Placing Script

From Noisebridge
Revision as of 14:01, 14 November 2015 by 167.220.25.98 (talk) (updated code with comments -Trevor)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Block Placing[edit]

Have you ever wanted to let the player place blocks, like in a game like minecraft?

Well now you can! BlockPlacer has been created to let you place blocks to your heart's content.

Detailed comments have been included in the script so that you can look into how it works. The full explanation is too lengthy for a tutorial.

Click this to get BlockPlacer.cs

Adding it to your project[edit]

Copy the entire script from the pastebin. Then go to unity, click create in the project box/window, and click C# script. Name it BlockPlacer. Double-click it to open up the script, and replace everything with the code you copied. Save it, and go back to unity.

Go to your player in the hierarchy, and click the down arrow next to them. Then click FirstPersonCharacter. Drag the BlockPlacer script into the inspector.

Create a new cube. Drag the cube into the project panel. Then delete the cube from the scene. Go back to where you put the BlockPlacer script, and drag the cube from the project panel into the box that has the label "Block Prefab".

You're done!

Writing code for BlockPlacer[edit]

In order to place blocks, you need to write some code to get blocks. Alternatively, you could just click the Unlimited Blocks checkbox, but where's the fun in that? Make a block that you can pick up, much like you made coins in that tutorial. Instead of adding a coin when you pick it up, make write:

BlockPlacer.addBlocks();

That will give you a block that you can now place.

Now that you can collect blocks, you can make all sorts of puzzles and challenges that require placing blocks to get around!