Search Unity

Movement loops with coroutines

Discussion in '2D' started by PlayKiseki, Oct 30, 2014.

  1. PlayKiseki

    PlayKiseki

    Joined:
    Oct 4, 2014
    Posts:
    14
    Hi all,

    I'm trying to implement a system where my gameObjects move in a "path" or loop using coroutines and rigidbody.addvelocity. It works kind of like below:


    The only problem is that my code is kind of... a mess. It uses public Vector2 & floats for travel time with a repeating pattern of coroutines and an int to determine whether it loops back to the first step on each one. I was wondering if anybody had any suggestions on how to clean it up a little and make it more workable. I tried using arrays for vectors and waitTime but I couldn't figure out how to start a coroutine for undeclared (in code) variables.

    If anybody could offer any suggestions on improving this code, I'd greatly appreciate it!

    Here is the code:

    Code (CSharp):
    1.  
    2.         public Vector2 posOne = new Vector2 (0, 0);
    3.         public float waitOne = 1.0f;
    4.         public Vector2 posTwo = new Vector2 (0, 0);
    5.         public float waitTwo = 1.0f;
    6.         public Vector2 posThree = new Vector2 (0, 0);
    7.         public float waitThree = 1.0f;
    8.         public Vector2 posFour = new Vector2 (0, 0);
    9.         public float waitFour = 1.0f;
    10.         public Vector2 posFive = new Vector2 (0, 0);
    11.         public float waitFive = 1.0f;
    12.         public Vector2 posSix = new Vector2 (0, 0);
    13.         public float waitSix = 1.0f;
    14.         public Vector2 posSeven = new Vector2 (0, 0);
    15.         public float waitSeven = 1.0f;
    16.         public Vector2 posEight = new Vector2 (0, 0);
    17.         public float waitEight = 1.0f;
    18.         public Vector2 posNine = new Vector2 (0, 0);
    19.         public float waitNine = 1.0f;
    20.         public Vector2 posTen = new Vector2 (0, 0);
    21.         public float waitTen = 1.0f;
    22.         public Vector2 posEleven = new Vector2 (0, 0);
    23.         public float waitEleven = 1.0f;
    24.         public Vector2 posTwelve = new Vector2 (0, 0);
    25.         public float waitTwelve = 1.0f;
    26.         public int loopOnStep = 12;
    27.         public float accelerationForce = 10;
    28.  
    29.         void Start ()
    30.         {
    31.                 StartCoroutine ("firstPos");
    32.  
    33.         }
    34.  
    35.         IEnumerator firstPos ()
    36.         {
    37.                 rigidbody.velocity = posOne;
    38.                 yield return new WaitForSeconds (waitOne);
    39.                 if (loopOnStep == 1) {
    40.                         StartCoroutine ("firstPos");
    41.                 } else {
    42.                         StartCoroutine ("secondPos");
    43.                 }
    44.         }
    45.  
    46.         IEnumerator secondPos ()
    47.         {
    48.                 rigidbody.velocity = posTwo;
    49.                 yield return new WaitForSeconds (waitTwo);
    50.                 if (loopOnStep == 2) {
    51.                         StartCoroutine ("firstPos");
    52.                 } else {
    53.                         StartCoroutine ("thirdPos");
    54.                 }
    55.         }
    56.  
    57.         IEnumerator thirdPos ()
    58.         {
    59.                 rigidbody.velocity = posThree;
    60.                 yield return new WaitForSeconds (waitThree);
    61.                 if (loopOnStep == 3) {
    62.                         StartCoroutine ("firstPos");
    63.                 } else {
    64.                         StartCoroutine ("fourthPos");
    65.                 }
    66.         }
    67.  
    68.         IEnumerator fourthPos ()
    69.         {
    70.                 rigidbody.velocity = posFour;
    71.                 yield return new WaitForSeconds (waitFour);
    72.                 if (loopOnStep == 4) {
    73.                         StartCoroutine ("firstPos");
    74.                 } else {
    75.                         StartCoroutine ("fifthPos");
    76.                 }
    77.         }
    78.  
    79.         IEnumerator fifthPos ()
    80.         {
    81.                 rigidbody.velocity = posFive;
    82.                 yield return new WaitForSeconds (waitFive);
    83.                 if (loopOnStep == 5) {
    84.                         StartCoroutine ("firstPos");
    85.                 } else {
    86.                         StartCoroutine ("sixthPos");
    87.                 }
    88.         }
    89.  
    90.         IEnumerator sixthPos ()
    91.         {
    92.                 rigidbody.velocity = posSix;
    93.                 yield return new WaitForSeconds (waitSix);
    94.                 if (loopOnStep == 6) {
    95.                         StartCoroutine ("firstPos");
    96.                 } else {
    97.                         StartCoroutine ("seventhPos");
    98.                 }
    99.         }
    100.  
    101.         IEnumerator seventhPos ()
    102.         {
    103.                 rigidbody.velocity = posSeven;
    104.                 yield return new WaitForSeconds (waitSeven);
    105.                 if (loopOnStep == 7) {
    106.                         StartCoroutine ("firstPos");
    107.                 } else {
    108.                         StartCoroutine ("eighthPos");
    109.                 }
    110.         }
    111.  
    112.         IEnumerator eighthPos ()
    113.         {
    114.                 rigidbody.velocity = posEight;
    115.                 yield return new WaitForSeconds (waitEight);
    116.                 if (loopOnStep == 8) {
    117.                         StartCoroutine ("firstPos");
    118.                 } else {
    119.                         StartCoroutine ("ninthPos");
    120.                 }
    121.         }
    122.  
    123.         IEnumerator ninthPos ()
    124.         {
    125.                 rigidbody.velocity = posNine;
    126.                 yield return new WaitForSeconds (waitNine);
    127.                 if (loopOnStep == 9) {
    128.                         StartCoroutine ("firstPos");
    129.                 } else {
    130.                         StartCoroutine ("tenthPos");
    131.                 }
    132.         }
    133.  
    134.         IEnumerator tenthPos ()
    135.         {
    136.                 rigidbody.velocity = posTen;
    137.                 yield return new WaitForSeconds (waitTen);
    138.                 if (loopOnStep == 10) {
    139.                         StartCoroutine ("firstPos");
    140.                 } else {
    141.                         StartCoroutine ("eleventhPos");
    142.                 }
    143.         }
    144.  
    145.         IEnumerator eleventhPos ()
    146.         {
    147.                 rigidbody.velocity = posEleven;
    148.                 yield return new WaitForSeconds (waitEleven);
    149.                 if (loopOnStep == 11) {
    150.                         StartCoroutine ("firstPos");
    151.                 } else {
    152.                         StartCoroutine ("twelthPos");
    153.                 }
    154.         }
    155.  
    156.         IEnumerator twelthPos ()
    157.         {
    158.                 rigidbody.velocity = posTwelve;
    159.                 yield return new WaitForSeconds (waitTwelve);
    160.                 if (loopOnStep == 12) {
    161.                         StartCoroutine ("firstPos");
    162.                 } else {
    163.                         StartCoroutine ("firstPos");
    164.                 }
    165.         }