Search Unity

Coroutine runs even though I tell it not to?

Discussion in 'Scripting' started by Meijer, Jan 11, 2019.

  1. Meijer

    Meijer

    Joined:
    Jun 11, 2015
    Posts:
    7
    Hello,

    I am making a script for my character so that he automatically moves to a certain point in my scene.

    Thing is, he keeps trying to get to the target position, even after already reaching it. I start my coroutine by saying if it has been used before (default set as false in the Start function), and set it to true once the coroutine is being used.

    My thoughts were that it shouldn't be possible to call it (therefore not moving the character), unless CoroutineUsed = false;? Is it something I'm just overlooking, or something I'm missing entirely?

    I thought of using if(transform.position == target.position){
    StopCoroutine("MoveTowardsPoint"), but that would obviously just pull my character back the moment he moved away...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. [RequireComponent(typeof(PlayerController))]
    5. public class CutsceneWalking : MonoBehaviour {
    6.  
    7.     //Variables----------------------------
    8.  
    9.     public Transform target;
    10.     public float speed;
    11.     public Animator anim;
    12.     private bool CoroutineUsed;
    13.  
    14.     //-------------------------------------
    15.  
    16.     private void Start()
    17.     {
    18.         CoroutineUsed = false;
    19.     }
    20.  
    21.     void FixedUpdate()
    22.     {
    23.         if (CoroutineUsed == false)
    24.         {
    25.             StartCoroutine("MoveTowardsPoint");
    26.         }
    27.  
    28.         if(CoroutineUsed == true)
    29.         {
    30.             StopCoroutine("MoveTowardsPoint");
    31.         }
    32.     }
    33.  
    34.     IEnumerator MoveTowardsPoint()
    35.     {
    36.         float step = speed * Time.deltaTime;
    37.         transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    38.         CoroutineUsed = true;
    39.         anim.SetBool("IsWalking", true);
    40.         yield return null;
    41.     }
    42. }
    Could anyone shed some light on my weird scripting here? (and by that I don't mean "Give me the answer/fix". I mean "Please give me some hints which might get me to the answer") :)
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    2,882
  3. jeffreyschoch

    jeffreyschoch

    Joined:
    Jan 21, 2015
    Posts:
    2,344
    I know you said not to just give you the answer, but you were pretty close anyway and I don't have a lot of time to give hints and check back. Depending on the exact behaviour you're looking to create, the implementation could differ, but you might find it easier using this approach:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(PlayerController))]
    6. public class CutsceneWalking : MonoBehaviour
    7. {
    8.  
    9.     //Variables----------------------------
    10.  
    11.     public Transform target;
    12.     public float speed;
    13.     public Animator anim;
    14.     private Coroutine activeCoroutine;
    15.  
    16.     //-------------------------------------
    17.  
    18.     void Update()
    19.     {
    20.         if (activeCoroutine == null)
    21.         {
    22.             activeCoroutine = StartCoroutine(MoveTowardsPoint());
    23.         }
    24.     }
    25.  
    26.     IEnumerator MoveTowardsPoint()
    27.     {
    28.         anim.SetBool("IsWalking", true);
    29.  
    30.         while (transform.position != target.position)
    31.         {
    32.             float step = speed * Time.deltaTime;
    33.             transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    34.             yield return null;
    35.         }
    36.    
    37.         anim.SetBool("IsWalking", false);
    38.         activeCoroutine = null;
    39.     }
    40. }