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.
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.
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.
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);
}
Post the code where you're using it.
– flaviusxviiPost more code so we can see what your doing exactly
– superventureYou're re-declaring your variables!
– SirGiveActually, 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.
– WolfosI 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...
– AtGfx