- Home /
Javascript Compile error
Hi, im starting to learn javascript and having problems with this small script. What im trying to do is when i press Fire2 i want my movement speed to increase. Can anyone help me find the problem within this script
#pragma strict
function Start(){}
function Fast ()
{
var speed = 0
if (Input.GetButtonDown ("Fire2"))
{
speed = 5;
}
}
function Update () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 1 + (speed);
var z = Input.GetAxis("Vertical") * Time.deltaTime * 1 + (speed);
transform.Translate(x, 0, z);
}
Answer by BiG · Jan 24, 2012 at 08:36 AM
OK, but what's the error that it gives to you? By the way, I've tried to modify your script. This would be the logic:
#pragma strict
var speed = 1;
function Fast (){ if (Input.GetButton("Fire2")) speed = 6; else speed = 1; }
function Update () { Fast(); var x = Input.GetAxis("Horizontal") Time.deltaTime speed; var z = Input.GetAxis("Vertical") Time.deltaTime speed; transform.Translate(x, 0, z); }
EDIT: The errors that you've done:
- You have declared Fast() function, but you have never called that. Now, The Update() function calls it every frame; before, it was never executed.
- Declare speed at the top of the script. You are using pragma strict, and the Update() function, that uses that variable, had no visibility of it.
- The speed formula was wrong to your purpose. You calculated Time.deltaTime 1 + speed, but you want Time.deltaTime (1 + speed). I've simplifed the calculation further, putting the speed assignment to value 6, directly.
Thank you, its playing now but the speed is not being constant only speeds up when clicked and not held down for somereason.
@jimkiller12, I edited my answer with the correct function, and I reported the errors that you've done. $$anonymous$$aybe this will help you next time!
Regards
Also, he forgot a semicolon at the end of the var speed = 0
line.
Answer by venkspower · Jan 24, 2012 at 09:18 AM
Where are you calling the Fast(). You should call the Fast() in the Update(), so that it will run every frame. And make the speed variable, you have initialized in the Fast function a global variable.
var speed = 0;
function Fast()
{
if (Input.GetButtonDown ("Fire2"))
{
speed = 5;
}
}
function Update()
{
Fast();
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(x, 0, z);
}
The script below works just fine, would there be a benefit of putting it in the Update function?#pragma strict
var speed = 1;
function Fast (){ if (Input.GetButton ("Fire2")) speed = 6; else speed = 1; }
function Update () { Fast();
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(x, 0, z);
}
Not really, no. The key point here is that the 'Fast' function gets called inside of Update anyway.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Exception error 1 Answer
BCE0049 error with network script 0 Answers
JScript problem 1 Answer