Search Unity

Resolved Why is my Coroutine not starting?

Discussion in '2D' started by Proxima_Centauri, Oct 25, 2020.

  1. Proxima_Centauri

    Proxima_Centauri

    Joined:
    Oct 20, 2020
    Posts:
    42
    I'm trying to make my character wall jump, so when I hit the wall and press jump, I add a Force which changes my character's x speed higher than its normal MaxSpeed, so the character is away from the wall and is not able to climb infinitely from it. Then, I want to return my MaxSpeed back to its normal value (I stored its default value in a variable in Start) gradually, so I'm using a Coroutine. This is the code I'm using

    Code (CSharp):
    1. maxSpeed = 6
    2. wallJumpSpeed = 15
    3. defaultMaxSpeed = maxSpeed (in Start) = 6
    4. h = Horizontal Axis input
    5.  
    6. //This is in the FixedUpdate
    7. if (wallJump && !touchGround)
    8.                 {
    9.                     maxSpeed = wallJumpSpeed;
    10.                     rb2d.velocity = new Vector2(0f, rb2d.velocity.y);
    11.                     rb2d.AddForce(Vector2.left * wallJumpSpeed * h, ForceMode2D.Impulse);
    12.                     wallJump = false;
    13.                     StartCoroutine(ReduceValue(maxSpeed, defaultMaxSpeed, 0.15f));
    14.                 }
    And this is the Coroutine

    Code (CSharp):
    1. public IEnumerator ReduceValue(float objective, float target, float reducer)
    2.     {
    3.         while (objective > target)
    4.         {
    5.             objective -= reducer;
    6.             yield return null;
    7.         }
    8.         objective = target;
    9.         yield return null;
    10.     }
    Now, when I make a Wall Jump, it works properly until the part when I want to reduce the speed, it's just not working and the MaxSpeed stays at the WallJumpSpeed. What am I doing wrong for the coroutine not to work? I have another item which boosts the speed for a moment and it works nicely by using the exact same Coroutine. Why is it not working with my WallJump?
     
  2. webox

    webox

    Joined:
    Aug 27, 2015
    Posts:
    72
    From what I can see, you're only modifying the "objective" float, which is a coroutine parameter, and that stays inside the coroutine, the modified value is never applied to a class variable or returned through yield or an out parameter. I'm not quite sure how is the other coroutine you mention working.
     
  3. Proxima_Centauri

    Proxima_Centauri

    Joined:
    Oct 20, 2020
    Posts:
    42
    I mean the Coroutine is referenced in other place, where I use a "boost" item, and then slows down the speed gradually, it's exactly the same, the objective is the current maxSpeed, the target is the defaultMaxSpeed, and thebreducer in still 0.15f.

    There, the Coroutine works properly, I don't know why it doesn't work in this case
     
  4. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    because floats pass as values and not as reference. change it like this:


    Code (CSharp):
    1.     public IEnumerator ReduceValue( ref float objective, ref float target, float reducer)
    2.         {
    3.             while (objective > target)
    4.             {
    5.                 objective -= reducer;
    6.                 yield return null;
    7.             }
    8.             objective = target;
    9.             yield return null;
    10.         }
    11.  
    and
    Code (CSharp):
    1.    StartCoroutine(ReduceValue(ref maxSpeed,ref defaultMaxSpeed, 0.15f));
     
  5. Proxima_Centauri

    Proxima_Centauri

    Joined:
    Oct 20, 2020
    Posts:
    42
    It doesn't work, the Coroutine method gives me an error

    Code (CSharp):
    1. public IEnumerator ReduceValue(ref float objective, ref float target, float reducer)
    2.     {
    3.         while (objective > target)
    4.         {
    5.             objective -= reducer;
    6.             yield return null;
    7.         }
    8.         objective = target;
    9.         yield return null;
    10.     }
    It tells me that (sorry if it's not translated well) the iterators can't have in, out or ref parameters
     
  6. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    true, well I guess you have to use the actual values in the coroutine then like this:

    Code (CSharp):
    1.     public IEnumerator ReduceValue(float reducer)
    2.         {
    3.             while (maxSpeed> defaultMaxSpeed)
    4.             {
    5.                 maxSpeed-= reducer;
    6.                 yield return null;
    7.             }
    8.             maxSpeed= defaultMaxSpeed;
    9.             yield return null;
    10.         }
    11.  
     
    Proxima_Centauri likes this.
  7. Proxima_Centauri

    Proxima_Centauri

    Joined:
    Oct 20, 2020
    Posts:
    42
    Yeah, now it works. I wanted to keep that Coroutine open to reduce any other value in case I needed it, but it seems it's not possible, so I'll stick with that one for now, I don't need to reduce other values anyway for now. Thank you very much!
     
  8. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    I guess you can do it if you wrap it in a class like this:

    Code (CSharp):
    1.    
    2. public class floatAsRef{
    3.  
    4. public float fv;
    5. }
    6.  
    7. public IEnumerator ReduceValue(floatAsRef objective, floatAsRef target, float reducer)
    8.         {
    9.             while (objective.fv > target.fv)
    10.             {
    11.                 objective.fv -= reducer;
    12.                 yield return null;
    13.             }
    14.             objective.fv = target.fv;
    15.             yield return null;
    16.         }
    17.  
    then you would need to have your maxSpeed defined as:

    Code (CSharp):
    1.  
    2. public floatAsRef maxSpeed = new floatAsRef();
    3. maxSpeed.fv = 120;//for example
    4.  
    5.  
    this is a workaround to pass value types as references into a coroutine, but its probably too complicated for your situation
     
    Proxima_Centauri likes this.
  9. Proxima_Centauri

    Proxima_Centauri

    Joined:
    Oct 20, 2020
    Posts:
    42
    Yeah, I'm getting started, so for now I'll leave it how it is. Thanks anyway!