How do I rotate the camera?
I am making a 3D spaceshooter where the camera changes from top-down to sidescrolling when i press a button. I have the code for moving it but I can't get it to rotate correctly. It needs to rotate to -90 in the Y-axis. This is my code for moving the camera ( very simply made )`
// Variables Vector3 moveToPosition; // This is where the camera will move after the start float speed = 0.05f; // this is the speed at which the camera moves bool started = false; // stops the movement until we want it
// Functions
void Start()
{
moveToPosition = new Vector3(10, 5, 4);
}
void Update()
{
if (Input.GetKeyDown("space"))
{
started = true;
}
// only want the movement to start when we decide
if (!started)
return;
// Move the camera into position
transform.position = Vector3.Lerp(transform.position, moveToPosition, speed);
}`
Comment
Best Answer
Answer by Hellium · Feb 05, 2019 at 09:25 AM
// Variables
// This is where the camera will move after the start
Vector3 endPosition;
// This is where the camera will move after the start
Quaternion endRotation;
// this is the speed at which the camera moves
float translationSpeed = 0.05f;
// this is the speed at which the camera rotates
float rotationSpeed = 0.05f;
// stops the movement until we want it
bool started = false;
// Functions
void Start()
{
moveToPosition = new Vector3(10, 5, 4);
endRotation = Quaternion.Euler(0, -90, 0) * transform.rotation;
// OR the following, I can't remember
// endRotation = transform.rotation * Quaternion.Euler(0, -90, 0);
}
void Update()
{
if (Input.GetKeyDown("space"))
{
started = true;
}
// only want the movement to start when we decide
if (!started)
return;
// Move the camera into position
transform.position = Vector3.Lerp(transform.position, endPosition, translationSpeed);
transform.rotation = Quaternion.Slerp(transform.rotation, transform.rotation, rotationSpeed);
}