Just Took the First Tut!

Hey, all! Just took the GUI tut. I am halfway through the beginner’s scripting tut, but I have reached a dead end!

I came over here because I thought it was a nice alternative to the BGE. I have scripted in both Python (Boo) and JavaScript before.

If any of you know the tutorial, when I try to put in the “Follow” JavaScript, it tells me that the script is not finished compiling. It just won’t compile!

Also, for the move script, I don’t get how it possibly works. There is no call in the script anywhere for keyboard input, but, yet, it works? I do not understand.

332483–11705–$cubecontrol_copyunity_214.zip (3.29 KB)

So no one knows?

You didn’t happen to rename or move it after saving did you?

Save your script, close the editor, then reopen it by double clicking it in Project

Thanks. I figured out what was wrong! But why is it that I can just define those variables in the “Move1” script and keyboard input is immediately applied? I am not new to coding, but I have never seen automatic keyboard input (?)

I’m not sure what you mean as I’m not familiar with this tutorial. But Unity does have some default keyboard input commands, you can see them in Edit > Project Settings > Input

You can alter existing input here, and also create new inputs by increasing the Size of the list.

mmm… Also, one thing I don’t understand is this:

var foo : Transform

Does this make the variable foo available in the inspector?

Yup it does, if you don’t specify whether or not it’s a public or private variable it’ll just automatically be public.

It’s generally a good idea to define all your variables as public or private tho.

alright… I would assume this would be so that readers of the script know?

var speed = 5.0;

function Update () {
	var x =Input.GetAxis("Horizontal")*Time.deltaTime*speed;
	var z =Input.GetAxis("Vertical")*Time.deltaTime*speed;
	transform.Translate(x,0,z);
}

Why is there no if statement in this script asking for a keypress?

The GetAxis calls return zero when there is no key input and the Translate call moves the object. If you move an object zero distance then nothing happens, so you don’t need an “if” statement in this case.

But how does

input.GetAxis(“Horizontal/Vertical”)

Make it know to go forward, left, right, and backward and to react when the corresponding keys are pressed?

Well, in the following snippet…

var speed = 5.0;

function Update () {

	// Convert input axes (key presses) to movement distance
	var x =Input.GetAxis("Horizontal")*Time.deltaTime*speed;
	var z =Input.GetAxis("Vertical")*Time.deltaTime*speed;

	// HERE IS WHERE THE MAGIC HAPPENS :)
	transform.Translate(x,0,z);

}

The transform.Translate call is doing the movement. If the keys are not pressed, then Input.GetAxis() returns zero, resulting in the expression Input.GetAxis(“Horizontal”)Time.deltaTimespeed evaluating to zero, which means… No movement.

If Input.GetAxis() returns any non-zero value, then transform.Translate() converts that to a relative distance to move.

Input.GetAxis() IS the statement checking for keypresses, essentially.

Basically, Unity lets us group input commands together sometimes. The commands behave like a 2-way momentary toggle. POSITIVE_KEY<->NEGATIVE_KEY
GetAxis is closer to a while statement than an if statement because it monitors the defined axis and responds when it is “tilted”. The axis is defined in Edit > Project Settings > Input like Fourthings said.
You don’t have to use the Input setup though, you can still link directly to the keys if you want. Some times it’s better since your script will work independent of the Project Settings.

So GetAxis does not actually get the axis? It retrieves the key(S) accordingly to the value after it?

Input.GetAxis()

Input class

It can get the axis for input devices like gamepad/joystick controllers and mouse, but in the case of keyboard input it works great as an abstraction that allows treating keyboard and device input in the exact same way. This really is quite convenient when you think about it.

.

Oh! So it just receives which keys mean horizontal and which mean vertical!!!

So why is it called GetAxis? It doesn’t get the axis of anything. Or is an axis another word for something?..
hmm…

So how is it plugged into Translate? I mean, what value does it return? Isn’t it a Boolean?

As you can see in the description, it does not return a boolean, it returns a value between -1 and 1. Because the keys are mapped such that one direction key returns a -1 and the opposite key returns a 1, it works just like an axis.

So if I said

var x = Input.GetAxis("Vertical")

update{

    transform.Translate(0,0,x)
    }

It would make the object do what? Go forward or backward when I pressed the up or down arrow?

how does it know what speed to go? Sorry that I am having such a hard time understanding this.

Yes, it would make the object move forward or backward, and probably extremely fast at that :slight_smile:

The speed is controlled by an expression like this one:

var x = Input.GetAxis("Vertical") * Time.deltaTime * speed;

In that expression Input.GetAxis() controls the direction, speed is the variable that controls how fast to move (given in units per second), and Time.deltaTime is a fractional number that represents the number of seconds since the last update, so when multiplied by the constant value in speed, it allows your object to move at the same speed on any machine (Time.deltaTime is often used for this purpose, because fast machines will return lower values with more frequent updates, and slow machines will return bigger values with fewer updates, but over a one second period the accumulated values always come out near one).

Example: If you supplied a value of 10 for speed, you would want your object to move at 10 units (meters?) per second. Time.deltaTime might, on any given call to this function, be equal to something like 0.03, and when multiplied by 10 would equal 0.3 units to move during this update (and when all updates over the full second are accumulated, it will equal the original 10). Now take the value returned by Input.GetAxis() (-1, 0, or 1) and multiply that by the previous 0.3 units, and you will see how it controls the direction.

I don’t think I wrote that clearly enough, I’m having some blood sugar issues at the moment, but if it’s not clear let me know and I’ll try to find a better explanation on the web :slight_smile:

Example of Time.deltaTime:

float accumulatedTime = 0;
float accumulatedSpeed = 0;
float speed = 23;
void Update()
{

	accumulatedTime += Time.deltaTime;
	accumulatedSpeed += Time.deltaTime * speed;
	if( accumulatedTime >= 1 )
	{
		print( "Accumulated time: " + accumulatedTime + ", Accumulated speed: " + accumulatedSpeed );
		accumulatedTime = 0;
		accumulatedSpeed = 0;
	}

}

Running this code resulted in the following output:

This illustrates that multiplying Time.deltaTime by any value will result, when accumulated over a one second period, that value (or near it), and this will hold true on nearly any target machine.

(Edited to add) I really should have called accumulatedSpeed something more relevant like accumulatedMovement, but it should still get the point across :wink:

.

oooohhh! thanks, guys! I get it now!