Beginner's Scripting Guide

From Unify Community Wiki
Revision as of 15:55, 7 December 2012 by TomoI (Talk | contribs)

Jump to: navigation, search


Contents

Introduction

So, you want to be a scripter, eh? Well, you came to the right place.
This tutorial introduces you to scripting with Unity, from any level of experience.
(Note: This tutorial does require the ability to make a scene.)
So, let's get started!

Basic JavaScript (Beginner)

function Start()
{
    print("Hello, this is the 1st section!");
}
Ok, so if you don't know what the 3rd line does, have a guess.

...Think you've got it? Good. It prints the words, "Hello, this is the first section!" to the console.
How incredibly useless is that? Very.

Making something move

Ok, so you can figure what we're gonna do now.
Create a new scene if you haven't already, and make a random cube/sphere, whatever. Just place it in front of the camera.
Now, click on the 'Create' button in the 'Project' view, and select 'JavaScript'. Name it what you like
Now, double-click on it, and wait.
When MonoDevelop has loaded up the file, delete everything and type in the following:

function Update()
{
    if(Input.GetMouseButtonDown(0))
    {
        print("Hang on a sec, you're probably wanting to know what this does.");
    }
}
Ok, so
function Update()

basically is something that is used once every frame, which in our case is 1/30 of a second.
More on that later. The curly braces are basically defining where the function begins and ends.

The next thing to explain is the if statement. In our case it is
if(Input.GetMouseButtonDown(0))
{
    print("CODE GOES HERE");
}

So it's like English. Basically it's saying, "If I find that mouse button '0' (left mouse button) is down, I'll do this."

Anyway, the code in the if is as follows:
transform.up += 1;

Plonk that in your if statement and SAVE YOUR FILE.
Basically, what that is, is the Y co-ordinate of our object, and we added 1 to it.
Now go back to Unity, and drag the script from the 'Project' view onto the sphere/cube etc. in the 'Hierachy' view.
Press the play button at the top of the editor and left-click your mouse in the 'Game' view. Twice.
If it moves, then your script works! Now exit the game and let's make this cube move down!
Enter this on a new line OUTSIDE the if statement, but still in Update().

if(Input.GetMouseButtonDown(1))
{
    transform.up -= 1;
}

You should know what that means now. It's just the right mouse button, not the left.

The whole file should look like this.
function Update()
{
    if(Input.GetMouseButtonDown(0))
    {
        transform.up += 1;
    }
 
    if(Input.GetMouseButtonDown(1))
    {
        transform.up -= 1;
    }
}

Feel free to edit this section/code as you please.

JavaScript vs. C# vs... seriously?

Now, I know wiki articles aren't meant to be biased, but this is just a statement of fact. Boo is the least popular choice for coding.
C# is the most popular/beneficial, and JavaScript is good for beginners but second popular. (Results from the coding articles)

Boo

So, why use C# or JS? Why not Boo?
Well Boo is a cousin of Python, which, although has a tidy syntax, is a bit confusing for JS/C# users. For example:

if 1 > 0:
    print("This bit is normal enough, but look at that if!")
print("App done!")

It's not all bad though. If it's what you're used to, then it's fine.

C#

This is a decent 'language'. It's ALMOST the same as JS, but it uses 'public', and 'private' etc.

An example of this is:
public void Start()
{
    print("This language is much more complex than Boo or JS, but for some reason it's very popular.");
}

Why this is the case is beyond me, but it's true.

Our pal, JavaScript

Nothing beats this for beginners, absolutely nothing.
No more said.

Instantiating

var fallingThing : Transform;
 
function Update()
{
    Instantiate(fallingThing, Vector3(0, 0, 0), Quaternion.identity);
}

Ok, well, don't bother guessing what that means if you are a newbie.
(Refusing to say n0.0b :D)
In a nutshell, that's what we're gonna be learning today.
So, what exactly is instantiating? Basically, it's creating something.

Instantiate(/* Var 1 = What to create. */ fallingThing, /* Var 2 = Where to create. */ Vector3(0,0,0), /* More on Vector3 later. Var 3 = HOMEWORK FOR YOU! For now put */ Quaternion.identity);

Ok, so let's start!

Making a 'Rigidbody' Appear

So, for starting, just create a cube. Don't move it, it's not worth bothering.
Now, click on 'Component>Physics>Rigidbody' at the top of Unity while your cube is selected.


Help

Help, I need a Loop Guru!

If you find you want to loop (i.e. for your co-routines) but are not sure about what type of loop, you've come to the right place! Here are a list of useful example loops;

The for loop

Basic "for" loop; start value, end value, update expression (operator i increases on each update):

function Update() 
{
	for (var i=0; i < someNumber; i++)
	{
		something something;
	}
}

The for loop can also be used to iterate of the contents of collections (ie. lists and arrays)

function ToggleRenderers() 
{
	var aList = GetComponentsInChildren(Renderer);
	for ( var r in aList )
	{
		r.enabled= ! r.enabled;
	}
}

The while loop

A 'while' loop in a co-routine:

var target : Transform;
 
if (target)
{
	StartCoroutine("Lerp", 1.0);
}
 
function Lerp (time : float)
{
	var pos = transform.position;
	var rot = transform.rotation;
	var originalTime = time;
 
	while (time > 0.0)
	{
		time -= Time.deltaTime;
		transform.position = Vector3.Lerp(target.position, pos, time / originalTime);
		transform.rotation = Quaternion.Slerp(target.rotation, rot, time / originalTime);
		yield;
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox