- Home /
Rotating the camera over a period of time
Hi all!
I'm currently working on a small game where I would like to rotate the camera depending on the state of one of my variables. The game has 4 walls, here's a screenshot, all tagged according to which wall they are. When the player hits one of the walls the gravity changes to make it so that the wall acts as the ground in which the gravity pushes down on, I'd like to rotate the camera to reflect this.
The variable currently responsible for change in gravity is gravityState, when this = 1 the gravity acts down, 2 = left, 3 = up, 4= right. I've got no idea how to code a change in the camera's rotation so that it not only reflects gravityState, but also have the rotation happen over a small period of time.
Any help with the code would be greatly appreciated.
Answer by kaplica · Sep 13, 2017 at 12:12 PM
You have to access the camera's transform in order to manipulate it's position, angle etc.
To do something each frame you need to place the code in the update so it will look something like this:
void Update()
{
transform.Rotate(Vector3.forward, 15.0f * Time.deltaTime);
}
(This piece of code assumes that the script is on the camera's game object) If you are unsure how to access camera from another script etc, let me know.
So how exactly do I make it so that it rotates until it has reached a certain position then stop?
So for that, you need to constantly monitor the rotation from transform component and then you check if it reaches certain rotation or position then you stop.
for example:
float moveSpeed = 0.1f;
while(myGO.transform.rotation.y < 90)
{
myGO.transform.rotation = Quaternion.Lerp(myGO.transform.rotation, Quaternion.Euler(0, 90, 0), moveSpeed * Time.time);
}
myGO.transform.rotation = Quaternion.Euler(0, 90, 0);
Use a while loop to perform some kind of operation until some condition is met.
Also, you can perform this in Update or as a coroutine.
Coroutine is performed frame by frame,
IEnumerator RotateObject()
{
// Do Some rotations here
yield return null;
}
and then you can call it with StartCoroutine(RotateObject());
Your answer
Follow this Question
Related Questions
How to make the camera follow the player while still being able to be rotated? 1 Answer
Forward and back movements with a camera emulating an isometric view 1 Answer
Why is my camera not snapping 90 degrees? 0 Answers
Camera rotates when I run into a wall 0 Answers
How can i make the camera look up and down through keyboard keys? 2 Answers