Unity Block Placing Script: Difference between revisions

From Noisebridge
Jump to navigation Jump to search
Tpaley (talk | contribs)
Created page with "== Block Placing == Have you ever wanted to let the player place blocks, like in a game like minecraft? Well now you can! BlockPlacer.cs has been created to let you place blo..."
 
updated code with comments -Trevor
 
(7 intermediate revisions by one other user not shown)
Line 2: Line 2:
Have you ever wanted to let the player place blocks, like in a game like minecraft?
Have you ever wanted to let the player place blocks, like in a game like minecraft?


Well now you can! BlockPlacer.cs has been created to let you place blocks to your heart's content.
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.
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.


[http://pastebin.com/rZfSpB0m Click this to get BlockPlacer.cs]
[http://pastebin.com/140bv3kW Click this to get BlockPlacer.cs]


== Adding it to your project ==
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 ==
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.


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

Latest revision as of 14:01, 14 November 2015

Block Placing

[edit | edit source]

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 | edit source]

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 | edit source]

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!