Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I smoothly rotate the camera in unity?

I am making a 2d unity 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 run the game, it gives me an error. Anybody have tips on how I can (smoothly) rotate the camera from side to side?

like image 819
thelearnerofcode Avatar asked Dec 15 '22 18:12

thelearnerofcode


1 Answers

You can get rid of errors by changing your code to this:

void Update () {
    float rotateX = Random.Range (0, 50);
    transform.eulerAngles = new Vector3(0,0,rotateX);
}

And attaching script component containing it to the camera. But then it is rotating randomly all the time.

I'm not sure, from the question, what kind rotation do you want. But you can use for example this

void Update () {
    transform.Rotate(Vector3.forward, 10.0f * Time.deltaTime);
}

to rotate the camera smoothly. Just change to first parameter to the axis around what you want to rotate.

like image 91
maZZZu Avatar answered Dec 21 '22 23:12

maZZZu