Creating a Script Asset
Next to the Hierarchy window, you will see your blank script appear. Name it whatever you like.
Drag it and drop it onto the Main Camera within the Hierarchy window. This will attach the blank script to the camera. When you click again on the Main Camera, you should see your script appear amongst the other features of the camera.
To actually add the code, double click on the new script within your asset window or where you see it listed in the inspector (the instance of the script name with the little page next to it.) This will launch MonoDevelop, which is Unity’s script-editor tool.
Again, here is the code you will want to add.
Tada! Though we could have told Unity exactly what coordinates we wanted the camera to spin around within the script, this makes things much easier to change on the fly. In addition, you now have a script you can use differently for different situations. To put it into perspective, you may not want the camera to spin around 0,0,0 in your second scene if you use the script. This keeps you from having to write an entirely new file.
Hit your play button and enjoy the show. You’ve just programmed your first code within unity, and have secured an asset that you can use within any new scene you choose to create. From here, you are also able to move the location of the camera within the Scene window to change angles or distance. No matter where you put that camera, it will rotate around that 0,0,0 point in space.
If you aren’t too dizzy, uncheck the box next to the name of your script (the checkmark is pictured above). The script is still there, but it is now disabled. The spinning camera is cool, but we want to do something much more interesting now that we know how to create and attach scripts to game objects.
Let’s Move the Cube
There are a few different ways to get something to move around in Unity (G.I. R.Type uses “force” applied to a “Rigidbody”), but for now we want to stick with the easily understood – the Transform. As you can see within the unity inspector window, the “position” of the cube is currently 0,0,0. These are the X,Y, and Z coordinates. In the same terms explained above, the Vector3 of transform.position would be 0,0,0.
Lets make that changeable. In our new script, lets make it so that the UP arrow key increases the X value of the cube for as long as we hold it down.
Okay, so we have some new stuff to explain. What the complete script basically says is: ’For every frame that the Input key “up” is held down, add “1″ to whatever the value is for the Cube’s X position’.
Write it and save it. Your results will vary depending on where your camera is positioned, but if the cube moves when you press the up arrow, you’ve done it right. :) Next, we’ll add the other possible directions.
It’s common to maintain that the Y direction is up/down and the X direction is forward/backward. If you choose to go a different route, you totally can – it’s your universe. With this above code saved and added to the project, your cube can now move in four different directions. This is perfect for many basic setups, but customization might be key later on.