Thursday, February 10, 2011

Make It Move

This is going to be a quick tutorial on making an object, a prefab and assigning a script to it.

Alright starting from where we left off yesterday. Select the code you have in your game. Set it's position to 0,0,0 and rotation to the same. Now select your camera and set it's position to 10,0,0 and rotation to 0,0,0. This should have your cube in the center of the screen when you start your game.

Now Create a folder in your game assets menu and call it prefabs. Not right click on the folder and select Create -> Prefab. This creates a blank Prefab or Object Template for you. In your scene Hierarchy view select your cube and drag it onto the new Prefab you have created. Now whenever you want to create a cube all you have to do is insert your newly created prefab. Simple! Name your prefab whatever you want by right clicking and selecting rename. I used "Ship".

Now create a second new folder and call it Scripts. Within this folder we want to create a new JavaScript code. This is found under Create -> JavaScript. Now open your code and paste this code into the text editor:

var vspeed : float = 3;
var hspeed : float = 5;
var vvel : float = 0;
var hvel : float = 0;
var fric : float = 2;

function Update () {
hvel += Input.GetAxis("Horizontal")*hspeed*Time.deltaTime;
vvel += Input.GetAxis("Vertical")*vspeed*Time.deltaTime;

if (!Input.GetAxis("Horizontal"))
hvel /= fric;
if (!Input.GetAxis("Vertical"))
vvel /= fric;

//Update Position:
transform.position.z += hvel;
transform.position.y += vvel;
}
Save this code and rename the script as "FlightControls" (No Space!). Now we want to make our cube/ship run this script so that it flies around the room. To do this is very simple. Select your prefab object and then drag the script you created from the Asset menu to the Object Properties menu (the farthest one on the right). Just drop it on the object and it should be added to the object.

Now delete the cube you have in your game. To get it back simply drag the prefab you created onto the game scene. You should see a cube around your mouse as you do this. Once you have placed it make sure that you have set it's position to 0,0,0.

Now run your game.

You should have a cube that you can move with WASD or the Arrow Keys around your room. If you run the cube off the screen then don't worry you can always restart the game.

The movement controls in the script are controlled in the script. However, if you look at the script, you will see that the W,A,S,D or any of the arrow keys are referenced. This is because Unity maps key axis for you in the Input menu. To access and change these settings is very simple. Just go to Edit -> Project Settings -> Input. Then click the arrow next to Axes to open the menu and view all possible Axes the script could have used. Arrow keys in Unity are referenced by their direction. The Left Arrow is titled "left" (without quotes) while the Right one is titled "right" (Again without the quotes). This means that you can easily change the controls of your game without having to fish through your scripts.

Thats all for today. It looks like Tutorial Week might have to be extended seeing as tomorrow is Friday and we have barely gotten to the programming yet... I guess I have the time.