Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using several coroutines as following

Script1:

Reads an input values that are x,y,z etc.

passes x and there values to script2 for execution one by one

Script2:

a function receives that values and checks for each if it is x with positive values it calls XPOS coroutine else if y .....

then that coroutine moves objects accordingly but when it reaches yield return new WaitForSeconds(0.2f); it go back to Script1 and start execution next step then come back and start execution after yield return new WaitForSeconds(0.2f); . If Script1 is finish then the coroutines switches with one another during yield return new WaitForSeconds(0.2f);

following is codes I am using;

Script2 Code: Receiving values from Scipt1 and calling coroutine against each
private  void ObjSupport_PositionArrived(string Coordinate, double Value)
   {
       Debug.Log("Cordinate" + Coordinate);
       Debug.Log("Value" + Value);

       float ivalue = (float)Value;
       switch (Coordinate)
       {
           case "X+":
               {
                   if (ivalue == 0)
                   {

                   }
                   else
                   {

                       StartCoroutine("FuncXPlus", ivalue);

                   }

               }
               break;
           case "X-":
               {
                   if (ivalue == 0)
                   {

                   }
                   else
                   {

                       StartCoroutine("FuncXMinus", ivalue);
                   }
               }
               break;

           case "Z+":
               {
                   if (ivalue == 0)
                   {

                   }
                   else
                   {

                       StartCoroutine("FuncZPlus", ivalue);



                   }
               }
               break;
           case "Z-":
               {
                   if (ivalue == 0)
                   {


                   }
                   else
                   {

                       StartCoroutine("FuncZMinus", ivalue);

                   }
               }
               break;

       }

After that following are coroutine functions
IEnumerator FuncZPlus(float ivalue)
   {
       Vector3 Beemposition = Beem.transform.localPosition;
       float BeemCurrentXpos = Beemposition.y;
       Beemposition.y = Mathf.Clamp(Beemposition.y + ivalue, 0.04f, 0.07f);
       while (BeemCurrentXpos <= Beemposition.y)
       {
           BeemCurrentXpos += 0.0001f;
           Vector3 posX = new Vector3(Beemposition.x, BeemCurrentXpos, Beemposition.z);
           Beem.transform.localPosition = posX;

           yield return new WaitForSeconds(0.2f);

       }

   }

   IEnumerator FuncZMinus(float ivalue)
   {
       Debug.Log("FunZMinus");
       Vector3 Beemposition = Beem.transform.localPosition;
       float BeemCurrentYpos = Beemposition.y;
       Beemposition.y = Mathf.Clamp(Beemposition.y + ivalue, 0.04f, 0.07f);
       while (BeemCurrentYpos >= Beemposition.y)
       {
           BeemCurrentYpos -= 0.0001f;
           Vector3 posX = new Vector3(Beemposition.x, BeemCurrentYpos,Beemposition.z);
           Beem.transform.localPosition = posX;

           yield return new WaitForSeconds(0.2f);

       }

   }
   IEnumerator FuncXPlus(float ivalue)
   {
       Debug.Log("FunXplus");
       Vector3 positionX = HeadRight.transform.localPosition;
       float CurrentXpos = positionX.x;
       positionX.x = Mathf.Clamp(positionX.x + ivalue, -0.02f, -0.002f);
       HeadRight.transform.localPosition = positionX;



       while (CurrentXpos <= positionX.x)
       {
           CurrentXpos += 0.0001f;
           Vector3 posX = new Vector3(CurrentXpos, positionX.y, positionX.z);
           HeadRight.transform.localPosition = posX;

           yield return new WaitForSeconds(0.2f);

       }

   }


   IEnumerator FuncXMinus(float ivalue)
   {
       Debug.Log("FunXminus");
       Vector3 xposition = HeadRight.transform.localPosition;
       float CurrentXpos = xposition.x;
       xposition.x = Mathf.Clamp(xposition.x - ivalue, -0.02f, 0f);
     while (CurrentXpos <= xposition.x)
       {
           Debug.Log("X-While");
           CurrentXpos -= 0.0001f;
           Vector3 posX = new Vector3(CurrentXpos, xposition.y, xposition.z);
           HeadRight.transform.localPosition = posX;

           yield return new WaitForSeconds(0.2f);

       }

   }


What I have tried:

The codes i have given are what i have tried
Posted
Updated 30-Oct-20 21:10pm

I am not sure what you are trying to do exactly but it seems to me that the switch statement can be simplified. You are checking that ivalue is not zero in every case. Why not test for ivalue==0 just once on the first line of the method and return if it is?

Your coroutine functions are returning an IEnumerator, my preference would be to return an IEnumerable<float> then you can iterate over the collection using a simple foreach statement.

The method call StartCoroutine(“FuncXPlus”, ivalue) looks like itis going to look up a function and pass ivalue to it. A switch statement using the format introduced in C#8 can simplify matters considerably and give you the same functionality in a few lines of code - something like this.


C#
private static IEnumerable<float> SwitchCoordinateFunction(string coordinate, float value)
 {
     if (value == 0) return null;
     return coordinate switch
     {
         "X+" => FuncXPlus(value),
         "X-" => FuncXMinus(value),
         "Z+" => FuncZPlus(value),
         "Z-" => FuncXMinus(value),
         _ => null,
     };
 }
 //example useage
    string coordinate = "X+";
     float value = 2;
     var functionResults = SwitchCoordinateFunction(coordinate, value);
     if (functionResults == null) return;

     foreach (float result in functionResults)
     {
         Console.Write($"{result}, ");
     }
 
Share this answer
 
v2
Everything is going fine in my code but during the
yield return new WaitForSeconds(0.2f);

other codes are executed .And i dont want that.
 
Share this answer
 
Comments
Richard Deeming 2-Nov-20 11:15am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution and post a comment.

Do not post your comment as a new "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900