- Home /
How to rotate camera around object smoothly in fixed intervals
Basically I want the camera to rotate at 90 degree intervals upon keypress.
I have the following:
public GameObject targetObject;
private float targetAngle = 0;
const float rotationAmount = 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()
{
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;
}
}
This works fine, but my question is should I be applying Time.Deltatime at any point to ensure it happens smoothly?
Thanks.
Comment
Answer by Jespertheend2 · Oct 03, 2015 at 07:22 PM
All you have to do is multiply your rotationAmount
by Time.deltaTime
Like this
transform.RotateAround(targetObject.transform.position, Vector3.up, -rotationAmount * Time.deltaTime);
You might have to make your rotationAmount a bit higher, since Time.deltaTime is usually around 0,016667