I have a game which gets as input the degrees of rotation around the z axis that the game’s camera should have. If I want the game to rotate around the center point I can add a script to the camera with the following code.
Vector3 temp = gameObject.transform.eulerAngle;
temp.z = inputDegreesOfRotation;
gameObject.transform.eulerAngles = temp;
However, I don’t want the camera to rotate around the center, but around a given point. I’m aware of rotateAround, but it’s not what I’m looking for since I don’t want to perform a rotation on the camera to get a new euler angle. I already know the angle and just want to set it. What is the best way to do this?
You could use an Empty GameObject to do the job:
- Set it at where you want the Camera to rotate around
- Set the camera to where it should be rotated at
- child it to the empty
- rotate the empty by your degrees
- unchild the camera
- reset the empty for next time.
public void turnAround (Vector2 point, GameObject obj, float angle)
Vector3 point3 = new Vector3(point.x,point.y,0);
Vector3 axis = new Vector3 (0,0,1);
Thanks to everyone who answered, but unfortunately none of the previous answers worked correctly on my device. I ended up finding this solution:
Calculate translation from camera center to pivot point
Translate camera to pivot point
Set camera euler angles to zero
Rotate camera by the given degrees
Translate camera back to original position (negative of the translation calculated in the first step)