Tuesday 3 November 2015

An easy-to-follow C# Unity Tutorial!

Unity Tutorial:

Hello! And welcome to my easy-to-follow and quick Unity tutorial, perfect for beginners! First, create a new 3d project using any starter assets if you like. Now name the project ‘Spinning Cube’.
Once Unity’s loaded up, at the top toolbar click GameObject -> Create 3d Object -> Cube. Now a box should pop into your scene!
Click on the cube you just created, and press F to focus on it for a better look at the action!

With the cube selected, move your mouse over to the ‘Inspector’ view (it defaults to the right of the screen) and click the ‘Add Component’ button, then scroll to ‘New Script’. Rename this new script ‘Spin’, and finally click ‘Create and Add’.
This should make a new script file in your selected folder (mine was ‘Assets’).
Now double-click the new icon to launch MonoDevelop to add some code! Hooray!
Now some basic code should pop-up on your screen in a separate window under ‘spin.cs’.
First, delete the text highlighted here:
Next, make a space with enter both above and below where you deleted, and on line 6 type: ‘public float speed = 10f;’

Now, on row 9 below void update type this: ‘transform.Rotate(Vector3.up, speed * Time.deltaTime);’
Lastly go to File -> Save (or use CTRL + S) to save your work and go back to the separate Unity tab.


Now finally, press the Play button at the top of the screen and BAM, your box will be rotating! Congratulations!
And now: as an extra-juicy tip which acts as the icing on our freshly-baked delicious spinning cake, if you head on back to the ‘Inspector’ tab we used earlier you’ll see a brand-new public variable has arrived! Can you spot what it is?

If you didn't guess it, it’s the Spin (script) section, allowing you to adjust the cube speed at will without opening up MonoDevelop again and finding/ modifying clunky code. Funky! This is because of the 10f int value we added to the rotation earlier, it added a new public variable that can even be changed while the game is playing!



Thanks again for following this user-friendly and beginner’s tutorial, and I hope it helped you come one-step closer to programming in C# with Unity! Tah-tah for now!

No comments:

Post a Comment