- Home /
Coroutines with Yield return new waitforseconds(); go for execution of other functions?
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 afteryield return new WaitForSeconds(0.2f);
. If Script1 is finish then the coroutines switches with one another duringyield 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 the 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);
}
}