Hey, Targos.
I wrote you a tutorial, it got deleted with the other stuff when the forums crashed, but David posted a backup so I shall replace it.
I am glad you are willing to give coding a shot. In the words of a wise man; This stuff is just too cool to give up on. icon_wink.gif ¨
Here is the classic rotating cube script. You create a cube by clicking GameObject->Create Other->Cube, then you drag this .js script onto it.
Ok, first simply making the cube rotate:
function Update()
{
transform.Rotate(0, 10, 0);
}
The first word “function” tells the compiler that the next code block is a function. Code blocks start with an { and end with an }.
So after “function” we have our function name “Update”. “Update” is a function that get’s called by unity every frame. Remember, case is important.
After our function name we have (). The () brakets are used to send data to the function if needed. Since we aren’t sending anything, we do (). Every function needs at least ().
Ok, We have finished telling the compiler that we have a function named Update that we are not sending any data to, now the next line is our {. Remember that { starts a function code block? The { is telling the compiler where your function code begins.
Next we have this line: transform.Rotate(0, 10, 0);
transform is a class is that manages the objects position, rotation, scale, etc. Since we are putting this script right onto our object, we just call transform. Next is a . and the word Rotate. Rotate’s first letter is uppercase because it is a function. After Rotate we have this (0, 10, 0). Remeber how I said the () are used to send data to a function? Well, here we are sending z,y and z to the function. The first number is the x axis, we set it to 0 because we don’t want to rotate on the x axis. The next number is the Y axis, and we set it to 10. 10 units per frame. Next is Z, and again we set it to 0. We just told the Rotate function to rotate around the Y axis (the middle number) 10 units per frame.
Right after the transform.Rotate line is “;” The semi colon is put at the end of most code you write. Take a look at other people’s code to see where it is used and where it isn’t.
Next comes the }. That closes the function code block, it tells unity that you have finished writing your function code.
So when you put the script on a cube and hit play, the cube will rotate.
But, what if we rand the code on a super slow machine? Since it is rotating 10 units every frame, wouldn’t lower framerates cause the cube to rotate slower? Yes it would. But there is a fix:
function Update()
{
transform.Rotate(0, 10 * Time.deltaTime, 0);
}
We multiplied (*) 10 by Time.deltaTime. Time is a class that provides…you guessed it, time functions. Time.deltaTime is float (number with a decimal point: 1.5, 2.333452) that contains the amount of time that has passed since the last frame. So by multipliying our speed number, 10, by Time.deltaTime, we ensure that the cube will spin at the same speed regardless of framerates.
Now try making the cube rotate on another axis, ot two at once.
I wish I could write more, but I have to get back to programming myself.
HTH,
-Jeremy
PS: Thanks for the backup David.