RotateAround Co-Routine to Exact Angle

Hi All,

Could someone point out what I am doing wrong in this Co-Routine where I want to rotate around a point and end up “exactly” rotated around by the Angle supplied.

IEnumerator RotateAround(GameObject gameobject, Vector3 point, Vector3 axis, float angle, float inTimeSecs, Action onComplete) {
		float t = 0f;
		for(t = 0f; t + Time.deltaTime < inTimeSecs; t += Time.deltaTime) {
			Debug.Log (string.Format ("{0}t,{1}atr", t, angle * Time.deltaTime));
			gameobject.transform.RotateAround (point, axis, angle * Time.deltaTime);
			yield return null;
		}

		gameobject.transform.RotateAround (point, axis, (inTimeSecs - t) * angle);

		if (onComplete != null) onComplete();
	}

Called with -

StartCoroutine (RotateAround (
				this.gameObject, 
				CameraRotatePoint.transform.position, 
				Vector3.right, 
				90f, 
				1f,
				() => { // Stuff }));

My rotation seems to hit 90 on first call, but if I then I call it with -90f it does not get to 0 but is X degrees out (and this varies with each loop).

Appreciate any help.

Just to make sure we are both clear. Using RotateAround will cause your object to circle around a paticular point somwhere else in space (I believe it keeps you facing that point) not actually rotate the object in place (spin around).

Using a for loop to iterate the time is confusing to me I’d do it like this:

IEnumerator RotateAround(GameObject gameobject, Vector3 point, Vector3 axis, float angle, float inTimeSecs, Action onComplete) {
         float currentTime = 0.0f;
         float angleDelta = angle/inTimeSecs; //how many degress to rotate in one second
         float ourTimeDelta= 0;
         while (currentTime< inTimeSecs)
         {
                  currentTime+=Time.deltaTime;
                  ourTimeDelta = Time.deltaTime;
                  //Make sure we dont spin past the angle we want.
                  if (currentTime > inTimeSecs)
                       ourTimeDelta-= (currentTime-inTimeSecs);
                  gameObject.transform.RotateAround(point,axis,angleDelta*ourTimeDelta);
             yield return null;
         } 
         if (onComplete != null) onComplete();
     }

you’ll probably never get it using degrees. your converting silently in the background from angles to what rotations really are which is quaternions. Your actually rotating in less time than you stated because its < than not <= though it doesn’t appear that should matter other than speeding it up maybe?

but maybe thats why?

ultimately though you probably have to use quaternions.