Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jimkiller12 · Jan 24, 2012 at 08:30 AM · javascripterrorcompile

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);
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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:

  1. You have declared Fast() function, but you have never called that. Now, The Update() function calls it every frame; before, it was never executed.
  2. 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.
  3. 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.
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jimkiller12 · Jan 24, 2012 at 08:40 AM 0
Share

Thank you, its playing now but the speed is not being constant only speeds up when clicked and not held down for somereason.

avatar image BiG · Jan 24, 2012 at 08:42 AM 0
Share

Try using GetButton() ins$$anonymous$$d of GetButtonDown().

avatar image jimkiller12 · Jan 24, 2012 at 08:44 AM 1
Share

That worked, Thank you.

avatar image BiG · Jan 24, 2012 at 08:50 AM 0
Share

@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

avatar image rabbitfang · Jan 24, 2012 at 09:30 PM 1
Share

Also, he forgot a semicolon at the end of the var speed = 0 line.

avatar image
0

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);
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jimkiller12 · Jan 24, 2012 at 10:21 AM 0
Share

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);

}

avatar image syclamoth · Jan 24, 2012 at 10:31 AM 0
Share

Not really, no. The key point here is that the 'Fast' function gets called inside of Update anyway.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Exception error 1 Answer

BCE0049 error with network script 0 Answers

JScript problem 1 Answer

Need Help Understanding Script! (Javascript) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges