Smoothly rotate camera

So, I’ve tried to get this to work for some time now.

Camera.main.transform.Rotate(0, -90, 0);

This rotates the camera the way i want to.
But I want the player to see that the camera rotates.
When I use that, the camera move in one frame, so the player cant see that it moves.

Is there a way to “animate” that movement, and also change this

followTarget.offset = new Vector3(8f, 2f, 0f);

to this

followTarget.offset = new Vector3(0f, 2f, -8f);

In the same “animation” ? The followTarget is just the script aht comes with unity, where the camera follows its target(duh :P)

Thanks for any advice!! :slight_smile:

Before anyone asks, I’ve searched on the forums and on google for this for atleast 2 hours. I havent found anything that can make this work :S

You’ll either want to use a Lerp or a timer to apply the rotation over a number of frames.

I recommend that you take a look at this answer:

I have thrown together an example of a timer below.
I haven’t tested it, so it may not be perfect!

float currentTime = 0f;
float timeToMove = 0.1f;  //the time taken to rotate.
bool isRotating = false; // set this bool true where you wish (such as on input)

void Rotate()  { // call this in update
if (isRotating == true) {
transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, targetRotation, currentTime / timeToMove);

	if (currentTime <= timeToMove) {
		currentTime += Time.deltaTime;
	}
	else {
		isRotating= false; // turns off rotating
		currentTime = 0f; // resets this variable
	}
}

I would use Mathf.SmoothDamp so that you get that smooth movement. So it could look like this:

Camera.main.transform.Rotate.Mathf.SmoothDamp(0, -90, 0);

However I am not 100% sure if it would look like that, but I used Mathf.SmoothDamp a while ago and it was something like that. I would also consider using a rigidbody to get more of that smooth movement, or even just use something like a for loop and go -10 with an added delay after and repeat is 9 times however this is probably not going to look smooth unless you make that value small enough like -1 and repeat is 90 times but that is a little too messy and will probably cause lag.

So all in all, I would use a rigidbody or Mathf.SmoothDamp or maybe time.deltaTime

Anyway those are some ways I can think of so I hope it helps :slight_smile:

by using Vector3.SmoothDamp and Quaternion.Slerp, this would be easy

//Attach this to your Camera/anything you want to interpolate
public class SmoothFollow : MonoBehaviour
{
        //Insert your final position & rotation here as an empty Transform
	public Transform target;
	public float movementTime=1;
	public float rotationSpeed=0.1f;
	
	Vector3 refPos;
	Vector3 refRot;

	void Update ()
	{
		if(!target)
			return;
		//Interpolate Position
		transform.position = Vector3.SmoothDamp(transform.position, target.position, ref refPos, movementTime);
		//Interpolate Rotation
		transform.rotation =  Quaternion.Slerp(transform.rotation, target.rotation, rotationSpeed *  Time.deltaTime);
	}
}