Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ShadowNukePie · Nov 05, 2016 at 12:06 AM · movementloopsrepeating

How to use infinite loops or alternatively repeated tasks

I know this is all very bad code, but it's just a concept. I want to have something walk in a square so i've given it four points and positions and have it walk there. It should work if i ran it once, but I want it to work forever. How can i make this code work? Alternatively, how can I change it to have my object work between points forever.

Code:

 public class movement1 : MonoBehaviour {
 public static GameObject target;
 public Transform targetPos = target.transform;
 public Vector3 targetPosv3 = target.transform.position;
 public Transform targetPos2 = target.transform;
 public Vector3 targetPosv32 = target.transform.position;
 public Transform targetPos3 = target.transform;
 public Vector3 targetPosv33 = target.transform.position;
 public Transform targetPos4 = target.transform;
 public Vector3 targetPosv34 = target.transform.position;
 public float step = 100.0f * Time.deltaTime;
 // Update is called once per frame
 void Update () {
     Start:
     for (int intx = 1; intx < 30; intx++)
     {

         transform.LookAt(targetPos);
         transform.position = Vector3.MoveTowards(transform.position, targetPosv3, step);
         StartCoroutine(MyMethod());
     }
     for (int intx = 1; intx < 30; intx++)
     {

         transform.LookAt(targetPos2);
         transform.position = Vector3.MoveTowards(transform.position, targetPosv32, step);
         StartCoroutine(MyMethod());
     }
     for (int intx = 1; intx < 30; intx++)
     {

         transform.LookAt(targetPos3);
         transform.position = Vector3.MoveTowards(transform.position, targetPosv33, step);
         StartCoroutine(MyMethod());
     }
     for (int intx = 1; intx < 30; intx++)
     {

         transform.LookAt(targetPos3);
         transform.position = Vector3.MoveTowards(transform.position, targetPosv33, step);
         StartCoroutine(MyMethod());
     }
     StartCoroutine(MyMethod());
     goto Start;
 }
 IEnumerator MyMethod()
 {
     yield return new WaitForSeconds(0.01f);
 }

}

It doesn't work in start or in update. What should I do? Thank you very much.

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 doublemax · Nov 05, 2016 at 12:40 AM

Untested, i just moved the code a bit:

  public float speed = 100.0f;
  
 void Start()
 {
   StartCoroutine( "DoAnimation" );
 }
 
 IEnumerator DoAnimation()
 {
   while(true)
   {
     for (int intx = 1; intx < 30; intx++)
     {
       transform.LookAt(targetPos);
       transform.position = Vector3.MoveTowards(transform.position, targetPosv3, speed * Time.deltaTime);
       yield return new WaitForSeconds(0.01f);
     }
     
     for (int intx = 1; intx < 30; intx++)
     {
       transform.LookAt(targetPos2);
       transform.position = Vector3.MoveTowards(transform.position, targetPosv32, speed * Time.deltaTime);
       yield return new WaitForSeconds(0.01f);
     }
     
     for (int intx = 1; intx < 30; intx++)
     {
       transform.LookAt(targetPos3);
       transform.position = Vector3.MoveTowards(transform.position, targetPosv33, speed * Time.deltaTime);
       yield return new WaitForSeconds(0.01f);
     }
     
     for (int intx = 1; intx < 30; intx++)
     {
       transform.LookAt(targetPos3);
       transform.position = Vector3.MoveTowards(transform.position, targetPosv33, speed * Time.deltaTime);
       yield return new WaitForSeconds(0.01f);
     }
   }
 }

You can't precalculate "step" using Time.deltaTime. Time.deltaTime is dynamic and can be different for every frame.

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 ShadowNukePie · Nov 05, 2016 at 12:51 AM 0
Share

I have another script set up to follow the player in a certain radius, and this works but only if I'm in that radius. What should I do? It only faces towards them now but never actually moves. Thank you though, this is a big help. EDIT: I fixed it. I had to set the speed in the editor, it was stuck. Thanks!

avatar image Bunny83 ShadowNukePie · Nov 05, 2016 at 01:14 AM 0
Share

If your question is answered / solved, please accept his answer to mark the question as answered / solved.

avatar image ShadowNukePie Bunny83 · Nov 05, 2016 at 01:19 AM 0
Share

Thanks, I did that!

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

how can i repeat an if statment for x sec? 1 Answer

Where do I place the while(true) in the following script without causing logic errors in run time? 0 Answers

How does one continuously move an object forward without using velocity? 1 Answer

Characters moving in mid air after going up hills/mountains and won't come back down for unkown reason(s) 0 Answers

Unity2d - Player Movement. Character "jumps" when transition from idle > run 0 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