- Home /
Rotate an Object around a Pivot for a specific Angle
I have something I'm trying to do, which I can't find an existing answer to. I'm looking to rotate an object (the Camera in this case) around a given pivot point, for a specific angle (e.g. 90 degrees to the right), at which point it will stop rotating.
The issue I'm having is that I can't find an effective way to determine when the desired rotation has been reached. Whenever I try to add in a condition, due to the way the degrees are applied, I find that once it completes four loops (90 degrees each time) the condition either instantly evaluates to true because the desired rotation is the negative of the current rotation, or it never evaluates to true because the desired rotation can never reach the actual rotation.
void Update()
{
if (Input.GetKeyDown(KeyCode.E) && !Input.GetKey(KeyCode.Q) && !rotating)
{
targetRotation = transform.rotation.eulerAngles.y - 90f;
Debug.Log("Rotating to " + targetRotation);
StartCoroutine(RotateAroundPivot());
}
}
private IEnumerator RotateAroundPivot()
{
rotating = true;
while (Mathf.Abs(transform.rotation.eulerAngles.y) - Mathf.Abs(targetRotation) > 0.05f)
{
Vector3 rotation = new Vector3(0, -90f, 0);
transform.RotateAround(pivotPoint, rotation, 100 * Time.deltaTime);
yield return new WaitForEndOfFrame();
}
transform.eulerAngles = new Vector3(transform.rotation.eulerAngles.x, targetRotation, transform.rotation.eulerAngles.z);
rotating = false;
}
The above is my code so far, which works correctly up until the 4th rotation. In this case, the Camera is at rotation -45, and the desired rotation is +45, which in absolute values is true, so the camera snaps to a new rotation but doesn't pivot around the pivot point.
Answer by Narc0t1CYM · Mar 25 at 04:30 AM
Since you already know how much you are rotating in a frame in your RotateAroundPivot()
IEnumerator, you can just track that from the start of the method. In the Update
of your code, 90 degrees were mentioned so the while cycle is only running if rotatedDegrees <= 90
. In the while cycle's transform.RotateAround()
you specified 90 * Time.deltaTime
angles to turn in a frame, so that's exactly the number you want to add to your local rotatedDegrees
float.
Rest of the code is the same as it was :)
private IEnumerator RotateAroundPivot()
{
rotating = true;
float rotatedDegrees = 0;
while (rotatedDegrees <= 90f)
{
Vector3 rotation = new Vector3(0, -90f, 0);
float anglePerFrame = 90 * Time.deltaTime;
transform.RotateAround(pivotPoint, rotation, anglePerFrame);
rotatedDegrees += anglePerFrame;
yield return new WaitForEndOfFrame();
}
transform.eulerAngles = new Vector3(transform.rotation.eulerAngles.x, targetRotation, transform.rotation.eulerAngles.z);
rotating = false;
}
Thanks for the suggestion. I had to make some extra tweaks around the rotation and angle per frame, as I was sending slightly incorrect values here, due to a misunderstanding of the function. After playing around with it for a bit, I got it to work perfectly.
Of course, I just wanted to show the logic behind it. Besides, it's worth more for your own studies if someone just points you in the direction and you come up with your own solution and changes!