- Home /
Need help on making a semi-circle rotation
I need help on stopping a rotation when it makes a 180 when i activate a coroutine for the first time, then whenever i activate the coroutine again, to go another 180 and not to glitch back to the original 180. I scoured this code for an hour trying different things, and i can't find whats wrong with it and quite frankly, I'm stumped. This is what i have come up with.
void Start()
{
RotationDegree = 0;
Rotating = false;
Rot = false;
Rotatingto = 0;
RotationDegreeAdded = 180;
}
void OnCollisionEnter(Collision C)
{
RotationDegree = RotationDegree + RotationDegreeAdded;
Rotatingto = RotationDegree;
Rotating = true;
StartCoroutine (MyMethod ());
//Debug.Log ("Rotation Begun");
}
void Update()
{
if (Rotating) {
Vector3 RotationVector= new Vector3 (0.0f, 0.0f, RotationDegree);
Background.transform.Rotate (RotationVector * (Time.deltaTime));
Goals.transform.Rotate (RotationVector * (Time.deltaTime));
}
Debug.Log (RotationDegreeAdded);
Debug.Log (RotationDegree);
}
IEnumerator MyMethod(){
yield return new WaitForSeconds (1);
//Debug.Log ("Rotation Over");
Background.transform.rotation = Quaternion.Euler (0.0f, 0.0f, Rotatingto);
Goals.transform.rotation = Quaternion.Euler (0.0f, 0.0f, Rotatingto);
if (RotationDegree == 180) {
RotationDegree = 0;
RotationDegreeAdded = 360;
}
if (RotationDegree == 360) {
RotationDegree = 0;
RotationDegreeAdded = 180;
}
}
You're setting RotationDegree back to 0 if it equals 180?
Thats just so it stops rotation after a second. I want it to rotate another 180 from that when i do the thing again.
Answer by KillHour · Oct 26, 2015 at 09:54 AM
This is a generalized function that can handle any rotation you want over any amount of time.
IEnumerator RotateObj (GameObject spinner, float timeToRotate, Vector3 direction) {
float t = 0;
while (t < timeToRotate) {
spinner.transform.Rotate(direction * (Time.fixedDeltaTime / timeToRotate));
t += Time.fixedDeltaTime;
yield return new WaitForFixedUpdate ();
}
yield break;
}
"spinner" is the object you want to rotate, "timeToRotate" is how long you want the rotation to take, and "direction" is the amount of rotation in degrees for each axis.
For example:
RotateObj(myObject, 1.0f, new Vector3 (0, 180, 0));
This will rotate the object "myObject" by +180 degrees on the (local) y axis over a period of 1 second. If you call it a second time, it will rotate the object another 180 degrees from the position it ended up at.
Your answer
Follow this Question
Related Questions
LookRotation Between 2 Points 1 Answer
How to Find an Object close to where the Player is Looking 1 Answer
Turret script. Making it aim in heigth only. 1 Answer
Quaternions acting up 1 Answer