Search Unity

IEnumerator doing something wierd

Discussion in 'Scripting' started by bananaofdestiniez, Feb 19, 2018.

  1. bananaofdestiniez

    bananaofdestiniez

    Joined:
    Feb 9, 2018
    Posts:
    4
    here's the script
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Everything : MonoBehaviour {
    7.  
    8.     public Transform Boomerang;
    9.     public Transform Forward;
    10.     public Transform Right;
    11.     public Transform Left;
    12.     public Transform Backward;
    13.     public bool rightisgoing = false;
    14.     public bool leftisgoing = false;
    15.     public float a;
    16.     public float b;
    17.  
    18.     void Update () {
    19.         Boomerang.transform.Rotate (0, 5, 0);
    20.         if (Input.GetKey (KeyCode.RightArrow) && rightisgoing == false) {
    21.             StartCoroutine (BRight (1));
    22.         }
    23.         if (Input.GetKey (KeyCode.LeftArrow) && leftisgoing == false) {
    24.             StartCoroutine (BLeft (1));
    25.         }
    26.     }
    27.  
    28.     IEnumerator BRight(float a) {
    29.         rightisgoing = true;
    30.         aaa:
    31.         if (a <= 225) {
    32.             a++;
    33.             if (a <= 100) {
    34.             Boomerang.position = Vector3.MoveTowards (Boomerang.position, Forward.position, 0.1f);
    35.             }
    36.             if (a <= 150 && a >= 101) {
    37.             Boomerang.position = Vector3.MoveTowards (Boomerang.position, Right.position, 0.1f);
    38.             }
    39.             if (a <= 225 && a >= 151) {
    40.             Boomerang.position = Vector3.MoveTowards (Boomerang.position, Backward.position, 0.1f);
    41.             }
    42.             goto aaa;
    43.         } else {
    44.             a = 0;
    45.             rightisgoing = false;
    46.         }
    47.  
    48.         yield return new WaitForSeconds (0.0f);
    49.     }
    50.  
    51.     IEnumerator BLeft (float b) {
    52.         leftisgoing = true;
    53.         bbb:
    54.         if (b <= 100) {
    55.             Boomerang.position = Vector3.MoveTowards (Boomerang.position, Forward.position, 0.1f);
    56.         }
    57.         if (b <= 150 && b >= 101) {
    58.             Boomerang.position = Vector3.MoveTowards (Boomerang.position, Left.position, 0.1f);
    59.         }
    60.         if (b <= 225 && b >= 151) {
    61.             Boomerang.position = Vector3.MoveTowards (Boomerang.position, Backward.position, 0.1f);
    62.         }
    63.         if (b <= 225) {
    64.             b++;
    65.             goto bbb;
    66.         } else {
    67.             b = 0;
    68.             leftisgoing = false;
    69.         }
    70.         yield return new WaitForSeconds (0.0f);
    71.     }
    72. }
    73.  
    it just moves the boomerang forward, up, and to the right
    before I moved the script into a coroutine, it worked fine
     
    Last edited: Feb 19, 2018
  2. bananaofdestiniez

    bananaofdestiniez

    Joined:
    Feb 9, 2018
    Posts:
    4
    it moves the boomerang about (0.4, 0.4, 0.8)
    Boomerang coords were (0, 0, 0) (Empty parent of 2 child stretched spheres)
    Forwards coords (0, 5, 10) (Empty)
    Backwards coords (0, 0, 0) (Empty)
    Right coords (5, 3, 6) (Empty)
    Left coords (-5, 3 6) (Empty)
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
  4. bananaofdestiniez

    bananaofdestiniez

    Joined:
    Feb 9, 2018
    Posts:
    4
  5. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Those coroutines simply runs everything in a single frame, then exits. Those yield returns are pretty useless because the goto statements simply jumps you to the case labels and executes from there.

    Instead of using goto, use a loop instead with a yield return inside the loop.
     
  6. bananaofdestiniez

    bananaofdestiniez

    Joined:
    Feb 9, 2018
    Posts:
    4
    Thank you so much! I actually got it working and that's thanks to you. You were a big help and I never knew an online community this great. Thanks again, Derpy (My online name).
     
    TaleOf4Gamers and BlackPete like this.