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
1
Question by gilgada · Jan 04, 2012 at 03:49 PM · movementloopdelaywhilecatapult

Problem with while statement.

Hi, I have a physics based educational game that I am working on and I have a problem. I have a catapult that I should be able to move with the left and right arrow keys. Originally the player would have to keep pressing the keys in order to get it to continue moving but I have decided that for this, a while statement would be better than my original if. the problem was that as it is an update function, the while statement would push the catapult as far as it would allow in its conditions within a single frame. I wanted my catapult to move in increments when the keys are held. I decided to add a timer to split this up but it seemed ineffective.

here is my code:

var delayCount : float = 5.0f;

function Update() {

delayCount -= Time.deltaTime; if (delayCount<0) {

 gameObject.tag == "catapult main";

     while(Input.GetKeyDown ("left") && gameObject.transform.position.x > -10)
         {
         gameObject.transform.position.x -= 1;
         }
     while(Input.GetKeyDown ("right") && gameObject.transform.position.x < 10)
         {
         gameObject.transform.position.x += 0.001;
         }
     delayCount = 5.0f;
 }

}

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

1 Reply

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

Answer by aldonaletto · Jan 04, 2012 at 04:01 PM

You should not use while or for loops inside Update unless they are limited counting loops (loop for 5 times, for instance), because they will lock your game until the condition is satisfied. You could move smoothly using this:

var speed : float = 5.0f; // meters per second

function Update() { // why are you changing the tag inside Update? gameObject.tag == "catapult main"; // move this object at constant velocity = speed: if (Input.GetKey ("left") && transform.position.x > -10){ transform.position.x -= speed Time.deltaTime; } if (Input.GetKey ("right") && transform.position.x < 10){ transform.position.x += speed Time.deltaTime; } }

Comment
Add comment · Show 3 · 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 gilgada · Jan 04, 2012 at 04:10 PM 0
Share

Thanks for the reply :)

I have tried this and it seems to do the same as my previous if statements. The problem is that the catapult will only move once for each key press. What I am looking for is a way to get the catapult to continue moving for each second or so that the key is pressed down.

avatar image aldonaletto · Jan 04, 2012 at 04:38 PM 0
Share

I read your question too fast, and made the same mistake: you should use Get$$anonymous$$ey ins$$anonymous$$d of Get$$anonymous$$eyDown - Get$$anonymous$$eyDown returns true only during the current Update, while Get$$anonymous$$ey returns true while the key is pressed (answer edited to use Get$$anonymous$$ey).

avatar image gilgada · Jan 04, 2012 at 04:49 PM 1
Share

thankyou, that has done it :)

I really appreciate your help, from what I've seen, you are a big help on this forum.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Increment in while loop, with timeout 1 Answer

What part of my While Loop is causing the game to freeze? (C#) 2 Answers

Is there a way to have an object gradually move to one spot, move back, and then stop? 1 Answer

Breaking from a Loop 2 Answers

While Loop won't end when SendMessage was executed.. 2 Answers


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