To start off with make sure that your player model has a rigidbody attached to it, this will ensure that when your character jumps they will drop back down to the ground and not float up in the air. Make sure that your character is selected in the project and then click on the "Component" option at the top of the screen. From the menu that pops up click on the "physics" option and then select "rigidbody" from the top of the next list that pops up. It will also be a good idea to put a tick in the "x" box next to the Freeze Rotation property to make sure that your character doesn't fall forward when jumping which could in turn cause problems with the script.
Now this is set up you can move on to actually writing the code.
To start off with lets deal with the forward and backwards movements for your character. Right click in your assets folder and create a new Javascript. After that double click on it to open it up.
At the top of the script there will be a small piece of text and symbols as shown in the example below.
All of the code in this tutorial needs to be kept inside of the brackets that look like this { , } .
The first thing you need to do is set a speed for the movement of your character. Click on the empty space underneath where it says "#pragma strict" at the top of your script and write in the following piece of code:
var Speed : float = 0.3f;
Now this is all set up you can write the first piece of code to actually make your player do something. Click on the empty space in between the two strange shaped brackets and add in this next line of code:
if (Input.GetKey ("w"))
transform.Translate(Vector3.forward * Speed);
your code should now look something like this.
This piece of code will make your player move forward whenever the "w" key is pressed. If the speed is too fast for your liking feel free to change the value of the "Speed" variable that you added at the beginning of the tutorial. Now you have the forward control set up its time to add in the control to make your player move backwards, most of the code for this is the same as the previous code and there are only two pieces of the code that need to be changed. Click on the empty space next to the number 10 in your script and press enter. This creates a little bit of extra space on your script and at the same time makes it easier to read each new piece that you add. Click on the space next to the number 11 on your script and type out the same piece of code again but this time instead of writing "w" inside of the brackets change the w to an s and change the word "forward" to "back". Your script should now look like this:
Drag and drop this script onto your player and then press play to test out your script, press the "w" key on your keyboard to move forward and the "s" key on your keyboard to move backwards.Once you've tested your script re-open it to add the next piece of code. The next thing you need to add is some way to look around with your character. The easiest way to achieve this is to create a control that makes your character rotate when a button is held down. Underneath the Speed variable at the top add a new variable with the name Rotate and give it a value like the previous variable. This variable will control how fast your player rotates around. Make an extra bit of space underneath the previous piece of code and add in the piece of code below:
if (Input.GetKey ("a"))
transform.Rotate(0,0,Rotate);
You will probably notice that this piece of code is very similar to the previous pieces, there only a few small changes in this new piece of code. The most important one is the fact that the "Translate" has changed to "Rotate", this ensures that your player rotates when the button is held down. Once you've added the previous piece of code to your script your script should now look like this:
Next up you need to add in a piece of code which will make your character rotate in the opposite direction. Again much like the code for the movement controls the next piece is much the same as the previous one with a couple of changes, take the previous piece of code but this time replace the "a" with "d" and instead of writing just Rotate put a - symbol in front of the word Rotate. Now when you hit play and hold down the "d" button your character will rotate clockwise instead of anti-clockwise.
You're now pretty much done with this script and there is only one more thing to take care of which is your characters jump function. If we just add in a normal "transform.Translate" command the your character will still jump but if the button is pressed again whilst in the air your character will jump again and the jump will be unlimited, to stop this from happening you need to add in a boolean statement. This statement will work with another function in the script to make sure that the player can only jump when they are touching the ground. Scroll back to the top of the script where you added the previous variables and add the variable shown below:
var JumpBool : boolean = true;
Now that the variable is set up you need to add a new function, this function will decide whether or not the player is touching the ground. Underneath the previous if statement make some space extra space and add in the function shown below:
function OnCollisionStay (collisionInfo : Collision)
{
JumpBool = true;
}
This function will decide whether or not the player is touching the ground when the jump button is pressed and set the boolean value to true. As well as this function you will also need to add another function which will set the boolean value to false when the player is off of the ground which will then disable the jump control and stop the player from being able to jump an unlimited amount of times. Make some extra space underneath the function that you just added and then add in the function below:
function OnCollisionExit (collisionInfo : Collision)
{
JumpBool = false;
}
Now these two pieces of code have been added your code should now look like this:
if (Input.GetKeyDown("j")
Notice that unlike that previous pieces of code that had the "GetKey" function in them this one doesn't have a second bracket at the end of it, this is because we need to add a little bit of extra code to this before it will work properly. After the bracket press the spacebar and add two "&" symbols in afterwards then hit the spacebar again and add in the piece of code below:
JumpBool == true)
Adding in this extra piece of code will complete this line of code and now means that whenever the "j" key is pressed the code will also check to make sure that the "JumpBool" variable you defined earlier is true. Now to add in the final piece of code, press enter to create a new line after you have added in the previous piece of code and copy the code shown below:
transform.Translate(Vector3.up * 1);
Now that you've added in this code your player will jump when you press the "j" key as long as the player is touching the ground and the "JumpBool" variable is true. Now you've finished the script it should look like this:
One important thing to note in this tutorial is to make sure that when you export your model from whatever 3D programme you are using is to check the axis of your model and make sure that the "Z" axis is the front of your model and the "Y" axis is the top of your model, this will make the forward, backwards and rotation controls work properly for your character.