How to smoothly rotate the camera in unity?
I am making a 2d game with C #. Right now, I am trying to make the camera rotate and I am using this code:
rotateX = Random.Range (0, 50);
Camera.main.transform.eulerAngles = Vector3(0,0,rotateX);
But every time I try to start the game it gives me an error. Does anyone have any hints on how I can (smoothly) rotate the camera from side to side?
source to share
You can get rid of the errors by changing your code to this:
void Update () {
float rotateX = Random.Range (0, 50);
transform.eulerAngles = new Vector3(0,0,rotateX);
}
And add a script component containing it to the camera. But then it spins randomly all the time.
I'm not sure as of the question what kind of rotation you want. But you can use for example this
void Update () {
transform.Rotate(Vector3.forward, 10.0f * Time.deltaTime);
}
to smoothly rotate the camera. Just change the first parameter to the axis around what you want to rotate.
source to share
Don't use Camera.main. Write a class for your camera and then add it to the camera on stage. Use transform.rotation like this:
void Update ()
{
transform.rotation = Quaternion.Euler(y, x, z);
transform.position = Quaternion.Euler(y, x, z);
}
see this for more info: http://wiki.unity3d.com/index.php?title=MouseOrbitImproved
source to share