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 Kashiki · Dec 01, 2013 at 04:10 PM · errorwhile-loopsprintconditional

While Loop Infinite... Rookie Mistake

You know, this is a rookie mistake. But, swallowing my pride... please help. The while loop I put in is looping infinitely.

I originally wanted it to be... while (movement buttons are pressed){ allow for sprinting; change boolean "sprintable" to TRUE; }

So... this is just a question; should I have made this an IF statement instead of a while? I'll test that to make sure as well. However, I want to know if it would work.

 <code>#pragma strict
 private var sprintable : boolean;
 
 function Start () {
     
 }
 
 function Update () {
     // Initiates the sprintable boolean, if movement buttons are pressed down. If these are pressed, the user is
     // able to increase their speed by pressing shift alongside it.
     // currently creates an infinite loop...
     while(Input.GetKeyDown("w") || Input.GetKeyDown("a") || Input.GetKeyDown("s") || Input.GetKeyDown("d")){
         sprintable = true;
         Debug.Log('You are moving!');
     }
 }</code>

Thanks in advance.

Comment
Add comment · Show 1
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 Kashiki · Dec 01, 2013 at 04:18 PM 0
Share

Ah... fixed it. I used an IF statement ins$$anonymous$$d of while, and then nested the WHILE condition inside of the if. Easy fix, I guess ^^;

EDIT: Nope. Not fixed. God, I need practice...

1 Reply

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

Answer by KiraSensei · Dec 01, 2013 at 04:24 PM

Try this :

 #pragma strict
 
 private var sprintable : boolean;
 
 function Start () {
 
 }
 
 function Update () {
     // Initiates the sprintable boolean, if movement buttons are pressed down. If these are pressed, the user is
     // able to increase their speed by pressing shift alongside it. 
     // currently creates an infinite loop...
     if (Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d"))
     {
         sprintable = true;
         Debug.Log('You are moving!');
     }
 }

You also may use KeyCode.

To handle movement, you rarely need a "while" loop because the Update method is called at every frame.

EDIT : new code for several buttons (not tested) :

 #pragma strict
     
 private var sprintable : boolean;
     
 function Start () {
     
 }
     
 function Update () {    
     if (Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d"))
     {
         Debug.Log('You are moving!');
         if (Input.GetKey("e"))
         {
             sprintable = true;
             Debug.Log('And sprinting!');
         }
     }
 }

Comment
Add comment · Show 12 · 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 Kashiki · Dec 01, 2013 at 04:44 PM 0
Share

That's so true, actually. But, now I've run into another problem...

When I try to initiate sprinting (by printing something in the Debug log, it doesn't work. I'm assu$$anonymous$$g it's because I can't use two keys at once...? I can post the code if you wish.

avatar image KiraSensei · Dec 01, 2013 at 04:55 PM 0
Share

Yes, post your code please.

avatar image Kashiki · Dec 01, 2013 at 06:10 PM 0
Share

#pragma strict private var sprintable : boolean;

function Start () {

}

function Update () { // Initiates the sprintable boolean, if movement buttons are pressed down. If these are pressed, the user is // able to increase their speed by pressing shift alongside it. // solving... if(Input.Get$$anonymous$$eyDown("w") || Input.Get$$anonymous$$eyDown("a") || Input.Get$$anonymous$$eyDown("s") || Input.Get$$anonymous$$eyDown("d")){ sprintable = true; Debug.Log(sprintable);

     if(sprintable && Input.Get$$anonymous$$eyDown("w") || Input.Get$$anonymous$$eyDown("a") || Input.Get$$anonymous$$eyDown("s") || Input.Get$$anonymous$$eyDown("d")){
         if(Input.Get$$anonymous$$eyDown("e")){
             Debug.Log("You are sprinting...");
         }
     }
 }
 else if(Input.Get$$anonymous$$eyUp("w") || Input.Get$$anonymous$$eyUp("a") || Input.Get$$anonymous$$eyUp("s") || Input.Get$$anonymous$$eyUp("d")){
     sprintable = false;
     Debug.Log(sprintable);
 }

}

This is the code...

avatar image KiraSensei · Dec 02, 2013 at 10:40 AM 0
Share

Don't you need to keep the button down to move your character ?

I edited my answer to add the multi buttons, not tested, so there may be some problems, just tell me !

avatar image thef1chesser · Dec 02, 2013 at 11:51 AM 0
Share

since you reset the nb$$anonymous$$oveButtons every update, so every frame, you'll never sprint in just one direction.

Show more comments

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

19 People are following this question.

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

Related Questions

sprint and stamina problems 0 Answers

I need help with an error 1 Answer

Every script says "can not load script" and that there is no monobehavior in the file 2 Answers

Player Prefs, Adding Two Variables 1 Answer

Limit rotation for a statue puzzle 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