+= operator doesn't work (Javascript)

Newest code, speed just remains 0.
When I add var to the second one, speed doesn’t remain 0.

var speed = 0;
var accel = 10;
var x = 0;
var y = 0;
var z = 0;

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

EDIT: Never mind, it works now. What the problem was, is that “accel” is just too small to actually make a difference, Unity prints this as 0.

Post the code where you're using it.

–

Post more code so we can see what your doing exactly

–

You're re-declaring your variables!

–

Actually, I did keep the declarations on top. I'm really not a programming noob but it just doesn't work. I have programmed Actionscript 3, which is very similar, so I kind of know how declaring variables work. The point is, that when I don't use var (only for the declaration), nothing happens.

–

I don't think it is possible to get timing info in the shader. You can have a look to the hlsl/glsl documentation to verify it. But if you attach a script to a game object you can set a speed factor to control the speed parameter passed to the shader, which can be different per object...

–

4 Answers

4

var speed = 0;
var accel = 10;
var x = 0;
var y = 0;
var z = 0;

function Update () {
    speed = speed + Input.GetAxis("Vertical") * Time.deltaTime * accel;
    z += speed;

    transform.Translate(x, 0, z);
}

Note that you use var to tell the compiler about your variables. Once you have declared them don’t use the var keyword when you use the variable.

Yep, that's what I did. It solves the problem but it's still silly and I'm used to using +=. removing the var completely stops it from working somehow.

–

Keep the var in the declaration up top. Leave it out later when you use the variable.

–

The reason why removing var works is because by using var you are actually creating a new variable local to your method, when you obviously want to assign it to your class variable of the same identifier. This becomes a problem of scope.

–

Doesn't really make any sense to have "var z += speed;", because what should it add speed to? Zero? Infinity?

–

var speed : float = 10;

function Update () {

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

}

Thanks Mac! What is the standard datatype it uses? Integer?
Coming from Actionscript, I assumed Javascript was kind of the same in managing variables as Flash. It’s not. I’ll remember that it’s not Actionscript and I’ll follow some Javascript tutorials.

It inferes the type from the initialization value, if any (or from other expressions in the context). If you had written: <b>var speed = 0.0;</b> the compiler would assume speed as a float. Personally, I don't believe in type inference, so I always declare explicity all variable types (tempting Murphy's Laws is not a good practice...)

–

Sounds like you just need to if/else the input code so that only one button press is recognized on a single frame. Or add a queuing system for button presses.

–

var Speed : float = 0;

var accel : float= 10;

private var RelativeSpeed : float = 0;

private var x = 0;

private var z = 0;

function Update ()
{

Speed += Input.GetAxis("Vertical") *  Time.deltaTime * accel;

z = z + Speed;
Speed -= RelativeSpeed;
print(Speed);
transform.Translate(x, 0, z);

}