• 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 Tom212 · Aug 11, 2015 at 03:09 PM · loops

How can I fix my loop steps to the framerate?

Hi, I'm new to Unity so I apologize if my question is posed poorly.

I want to use a loop to execute the following until a condition is met:

transform.Translate (Vector3.right * (Time.deltaTime * speed));

However whenever I do so, rather than translating the object at a rate of speed per second it steps through the entire process before the frame can end.

Is there a way I can set the loop to execute one step per framerate? Or perhaps I'm being silly and there's a much better way to do this.

I assume what I really need for this is a timer but I'm not quite sure how to set one up other than putting a pause in each step.

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 Mako-Infused · Aug 11, 2015 at 03:33 PM

What you're looking for is: http://docs.unity3d.com/ScriptReference/MonoBehaviour.Update.html. "Update is called every frame". By placing your code inside the update method, it will be run every frame. You mentioned setting it up so it ran until a condition is met, to do that you can add a "while" statement with a condition.

If you're unsure of how to setup a monobehaviour for a script, you should consult the video learning series from the official Unity website: https://unity3d.com/learn/tutorials/topics/scripting.

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 Tom212 · Aug 11, 2015 at 09:25 PM 0
Share

Thanks for the answer, I figured I needed something to be in update but forgot to mention I was trying to put that code in a function and that was the problem.

I found the solution I needed was Coroutines.

Really appreciate your help nevertheless!

avatar image Mako-Infused · Aug 12, 2015 at 03:49 PM 0
Share

Oh, alrighty. I'm glad I could help!

Note: A coroutine really just makes the method asynchronous, as far as I understand it; so you'll still need to call the method from an "Update" if you want it to be fired each frame.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why does audio get out of sync with multiple loops playing? 1 Answer

Argument out of range 0 Answers

My loop isn't working??? (C#) (Need help please) 1 Answer

Why does this immediately freeze the simulation? 1 Answer

Using a for loop to generate and populate an array 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