- Home /
Rotating a camera 90 degrees around an object
I've attached this script to the camera and am using it to rotate the camera around an object. It's currently rotating freely but I'd like it rotate 90 degrees smoothly, over 60 frames, when the button is pressed. So that four presses of the left arrow key would have the have the camera rotate the full 360 degrees over 240 frames and back to it's starting position. I can't seem to figure out how to do this. Any help would be appreciated.
public GameObject targetObject;
public float rDistance = 1.0f;
public float rSpeed = 1.0f;;
// Update is called once per frame
void Update()
{
// Trigger functions if Rotate is requested
if (Input.GetKey(KeyCode.LeftArrow)) {
RotateLeft(true);
} else if (Input.GetKey(KeyCode.RightArrow)) {
RotateLeft(false);
}
}
protected void RotateLeft(bool goLeft)
{
float step = rSpeed * Time.deltaTime;
float orbitCircumfrance = 2F * rDistance * Mathf.PI;
float distanceDegrees = (rSpeed / orbitCircumfrance) * 360;
float distanceRadians = (rSpeed / orbitCircumfrance) * 2 * Mathf.PI;
if (goLeft) {
transform.RotateAround(targetObject.transform.position, Vector3.up, -distanceRadians);
} else {
transform.RotateAround(targetObject.transform.position, Vector3.up, distanceRadians);
}
}
Answer by EvilWarren · Mar 05, 2014 at 12:55 PM
This is a basic solution from what I understood of what you wanted, building on code you have supplied but hopefully it is enough for you to refine to what fits best.
public GameObject targetObject;
private float targetAngle = 0;
const float rotationAmount = 1.5f;
public float rDistance = 1.0f;
public float rSpeed = 1.0f;
// Update is called once per frame
void Update()
{
// Trigger functions if Rotate is requested
if (Input.GetKeyDown(KeyCode.LeftArrow)) {
targetAngle -= 90.0f;
} else if (Input.GetKeyDown(KeyCode.RightArrow)) {
targetAngle += 90.0f;
}
if(targetAngle !=0)
{
Rotate();
}
}
protected void Rotate()
{
float step = rSpeed * Time.deltaTime;
float orbitCircumfrance = 2F * rDistance * Mathf.PI;
float distanceDegrees = (rSpeed / orbitCircumfrance) * 360;
float distanceRadians = (rSpeed / orbitCircumfrance) * 2 * Mathf.PI;
if (targetAngle>0)
{
transform.RotateAround(targetObject.transform.position, Vector3.up, -rotationAmount);
targetAngle -= rotationAmount;
}
else if(targetAngle <0)
{
transform.RotateAround(targetObject.transform.position, Vector3.up, rotationAmount);
targetAngle += rotationAmount;
}
}
How come you never use the variable distanceDegrees, or distanceRadians in your example?
Wondering how to Rotate around the player, like a boom stick. I used this and it works well but stays in one position... so you can't see the player any more. Probably something easy to fix, just thought I'd ask in case someone else was having a similar problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135418im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotate Camera on axis and X axis not working 1 Answer
Logical Camera errors... 1 Answer
Multiple Cars not working 1 Answer
Movement with camera and rotation of objects 1 Answer
Get object orientation 3 Answers