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 cstabile18 · May 06, 2014 at 05:21 AM · transform.positionmethodwhilewhile-loopwhile loop

Using a while loops to repeatedly execute a method?

Okay so the code I provided below is a simplification of my actual code as I felt it would be more reasonable to post a simpler version and hopefully receive some answers that I can apply to my actual, fairly more complex code. Anyway, I am basically trying to use a while loop to repeatedly execute a method such that, on start, the game object that the script is attached to will traverse a smooth path (in this case linear). Below are two of my attempts at doing this. When I run it, the game object will either not move at all or will jump/teleport from start point to endpoint rather than traveling a smooth line from start to end. I do not understand why these do not work... Any ideas?

  float b = 0;
     
     void Start () {
     
          while (b < 1) {
     
               Function ();
           
          }
     
     }
     
     void Function () {
     
          x = b + 1;
          y = b + 2;
          transform.position = new Vector3 (x, y, 0);
          b += .01;
 
     }


     float b = 0;
     
     void Start () {
     
          while (b < 1) {
     
               transform.position = Function ();
           
          }
     
     }
     
     Vector3 Function () {
 
          Vector3 newPosition;
 
          x = b + 1;
          y = b + 2;
          newPosition = new Vector3 (x, y, 0);
 
          b += .01;
 
     }
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 iamvishnusankar · May 06, 2014 at 05:29 AM 0
Share

Are you looking for a smooth travel from Point A to Point B?

2 Replies

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

Answer by fafase · May 06, 2014 at 05:35 AM

you need to look into coroutine. At the moment, when the while loop starts it performs the whole movement within the Start method, so you don't see the progression only the final result.

With a coroutine, you can leave the method at a point and get back there next frame. In between the object is rendered:

 void Start(){
     StartCoroutine(Move());
 }
 IEnumerator Move(){
     float b = 0;
     while (b < 1) {
           transform.position = new Vector3 (b + 1, b + 2, 0);
           b += .01;
           yield return null;
      }
 }
Comment
Add comment · 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
0

Answer by TheRaven · May 06, 2014 at 05:33 AM

If you want to have an object animate around you will need to do the position changes in the update function instead of the start function.

So that the object moves the desired amount during an update call and is then rendered in the render call. Then moves more in the next update call and is then rendered in the next render call.

What you have done is move the object before anything is up and running, by the time an update and render is called the object is at the final position.

Comment
Add comment · 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

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

22 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 avatar image avatar image avatar image

Related Questions

Quaternion.Slerp issue 1 Answer

Iterator variable not increasing. 1 Answer

How to get something to be inactive while there is a certain value 2 Answers

Display a loading while processing a while loop 1 Answer

While loop freezes unity 3 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