# Rotating and Stoping in mid of rotation with co-routines

Hello,
I have an object that has 10 children which surround him in a circle.
There’s 5 object in-front of me at any given moment, and one of them is highlighted.
When I rotate my parent object by 36 degrees, they all move so the next one is highlighted (depending on left/right rotation).
I’ve managed to write properly the rotation with coroutine. I’m struggling now with stop the co-routine in the middle of the motion and return back to where it came from.
For example if I start from 0 to 36 and I want to stop in 18 degrees and move back to 0 (If I moved left and now decided to move right), it won’t work properly.
I’m trying to get the eulerangles.y when stop the motion but it’s not always a range between 1-35… It’s like 358, -21 and so on.

Here’s what I’ve done so far:

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ResultsRotator : MonoBehaviour
{

public bool doSwipe = false;
public bool swipeRight = false;
public float speed = 1f;
Quaternion rotation = Quaternion.identity;
float angle = 0f;
public bool rightSwipe = false;
public bool leftSwipe = false;
private Coroutine co;
public float currAngle = 0;
public float prevAngle = 0;
public float howMuchRotated = 0;
// Use this for initialization
void Start()
{
transform.eulerAngles = new Vector3(0, currAngle, 0);
}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("right"))
{
if (rightSwipe == false && leftSwipe == false)
co = StartCoroutine(ToRight(1f));
else if (leftSwipe == true)
{
StopCoroutine(co);
co = null;
howMuchRotated = transform.eulerAngles.y - prevAngle;
currAngle = transform.eulerAngles.y;
co = StartCoroutine(ToRight(1f));
leftSwipe = false;
}
}
if (Input.GetKeyDown("left"))
{
if (leftSwipe == false && rightSwipe == false)
co = StartCoroutine(ToLeft(1f));
else if (rightSwipe == true)
{
StopCoroutine(co);
co = null;
Debug.Log("A " + transform.eulerAngles.y);
Debug.Log("B " + prevAngle);
howMuchRotated = 0;
Debug.Log(howMuchRotated);
currAngle = transform.eulerAngles.y;
co = StartCoroutine(ToLeft(1f));
rightSwipe = false;
}

}
}

IEnumerator ToRight(float duration)
{
Debug.Log("Turning right");
Debug.Log("PrevAngle: " + prevAngle);
rightSwipe = true;
float startRotation = currAngle;
Debug.Log("Start Rotation: " + startRotation);
float endRotation;

if (startRotation % 36 != 0f)
{
Debug.Log("Rtest" + startRotation + " " + currAngle);
endRotation = startRotation - currAngle;
}
else
endRotation = startRotation - 36f;
float t = 0.0f;
Debug.Log("End Rotation: " + endRotation);
while (t < duration)
{

t += Time.deltaTime;
float yRotation = Mathf.Lerp(startRotation, endRotation, t / duration) % 360.0f;
transform.eulerAngles = new Vector3(transform.eulerAngles.x, yRotation, transform.eulerAngles.z);
yield return null;
}
prevAngle = endRotation + 36;
currAngle = endRotation;
rightSwipe = false;
co = null;
}

IEnumerator ToLeft(float duration)
{
Debug.Log("Turning left");
Debug.Log("PrevAngle: " + prevAngle);
leftSwipe = true;
float startRotation = currAngle;
Debug.Log("Start Rotation: " + startRotation);
float endRotation;

if (startRotation % 36 != 0f)
{
Debug.Log("Ltest " + startRotation + " " + currAngle);
endRotation = startRotation + (360 - currAngle);
}
else
{
endRotation = startRotation + 36f;
}

Debug.Log("End Rotation: " + endRotation);
float t = 0.0f;
while (t < duration)
{
t += Time.deltaTime;
float yRotation = Mathf.Lerp(startRotation, endRotation, t / duration) % 360.0f;
transform.eulerAngles = new Vector3(transform.eulerAngles.x, yRotation, transform.eulerAngles.z);
yield return null;
}
prevAngle = endRotation - 36;
currAngle = endRotation;
leftSwipe = false;
co = null;
}
}
``````

Almost solved. The only problem now is if I click fast left, right, left,right non-stop it does unknown behaviour… like always tries to rotate left/right a lil bit to every degree.

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ResultsRotator : MonoBehaviour
{

public bool doSwipe = false;
public bool swipeRight = false;
public float speed = 1f;
Quaternion rotation = Quaternion.identity;
float angle = 0f;
public bool rightSwipe = false;
public bool leftSwipe = false;
private Coroutine co;
public float currAngle = 0;
public float prevAngle = 0;
public float howMuchRotated = 0;
public float tempHowMuchRotated = 0;
public float startRotation = 0;
// Use this for initialization
void Start()
{
transform.eulerAngles = new Vector3(0, currAngle, 0);
}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("right"))
{
if (rightSwipe == false && leftSwipe == false)
co = StartCoroutine(ToRight(1f));
else if (leftSwipe == true)
{
StopCoroutine(co);
co = null;
tempHowMuchRotated = howMuchRotated;
Debug.Log("Prev :" + prevAngle);
currAngle = prevAngle + tempHowMuchRotated;
Debug.Log("curr :" + currAngle);
co = StartCoroutine(ToRight(1f));
leftSwipe = false;
}
}
if (Input.GetKeyDown("left"))
{
if (leftSwipe == false && rightSwipe == false)
co = StartCoroutine(ToLeft(1f));
else if (rightSwipe == true)
{
StopCoroutine(co);
co = null;
tempHowMuchRotated = howMuchRotated;
currAngle = prevAngle - tempHowMuchRotated;
co = StartCoroutine(ToLeft(1f));
rightSwipe = false;
}

}
}

IEnumerator ToRight(float duration)
{
Debug.Log("Turning right");
prevAngle = currAngle;
Debug.Log("PrevAngle: " + prevAngle);
rightSwipe = true;
startRotation = currAngle;
Debug.Log("Start Rotation: " + startRotation);
float endRotation;

if (startRotation % 36 != 0f)
{
Debug.Log("Rtest" + startRotation + " " + tempHowMuchRotated);
endRotation = startRotation - tempHowMuchRotated;
}
else
endRotation = startRotation - 36f;
float t = 0.0f;
Debug.Log("End Rotation: " + endRotation);
while (t < duration)
{
t += Time.deltaTime;
float yRotation = Mathf.Lerp(startRotation, endRotation, t / duration) % 360.0f;
howMuchRotated = Mathf.Lerp(0, 36, t / duration);
transform.eulerAngles = new Vector3(transform.eulerAngles.x, yRotation, transform.eulerAngles.z);
yield return null;
}
currAngle = endRotation;
prevAngle = startRotation;
rightSwipe = false;
co = null;
}

IEnumerator ToLeft(float duration)
{
Debug.Log("Turning left");
prevAngle = currAngle;
Debug.Log("PrevAngle: " + prevAngle);
leftSwipe = true;
startRotation = currAngle;
Debug.Log("Start Rotation: " + startRotation);
float endRotation;

if (startRotation % 36 != 0f)
{
Debug.Log("Ltest " + startRotation + " " + tempHowMuchRotated);
endRotation = startRotation + tempHowMuchRotated;
}
else
{
endRotation = startRotation + 36f;
}

Debug.Log("End Rotation: " + endRotation);
float t = 0.0f;
while (t < duration)
{
t += Time.deltaTime;
float yRotation = Mathf.Lerp(startRotation, endRotation, t / duration) % 360.0f;
howMuchRotated = Mathf.Lerp(0, 36, t / duration);
transform.eulerAngles = new Vector3(transform.eulerAngles.x, yRotation, transform.eulerAngles.z);
yield return null;
}
currAngle = endRotation;
prevAngle = startRotation;
leftSwipe = false;
co = null;
}
}
``````