- Home /
Rotate Camera to a position and rotate back
I am pretty new to unity and I want to rotate my camera to a random position and then rotate it back to it's original rotation. for now I just compute a random vector and apply rotation speed to it. But It does not return to it's original position and keeps rotating. The goal is to rotate the camera and rotate it back.
public Camera cam;
public Transform obj;
public float rotationSpeed = 0.5f;
Transform camPos;
Transform mainPos;
float camPitch = 0.0f;
bool val = true;
Vector3 rotationPos;
void Start()
{
camPos = cam.GetComponent<Transform>();
mainPos = camPos;
rotationPos = new Vector3(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f));
}
void LateUpdate()
{
if(camPitch < 20)
{
camPitch += rotationSpeed;
camPos.Rotate(rotationPos*rotationSpeed);
}
else
{
camPos.Rotate(mainPos.position * rotationSpeed);
}
}
Answer by Ark_Revan · May 23, 2017 at 12:43 PM
One way to do that, is to use a kind of timer.
as a global var
public float totalTime;
private float startTimer = 0;
and in the update
if(startTimer == 0){
startTimer = Time.time;
}
if(Time.time < totalTime/2){
//apply the velocity to rotate
}else if(Time.time > totalTime/2){
//apply -1*velocity to rotate
}else if(Time.time > totalTime){
startTimer = 0;
}
Thanks for the help! This indeed does rotate to the new position but it doesn't return to the original position and rotate again to a new position.
Did you wrote something like this in the first comment
float rotHorizontal = 1 * rotateSpeed * Time.deltaTime;
Camera.main.transform.Rotate(0, rotHorizontal, 0, Space.World);
and this in the second comment ?
float rotHorizontal = -1 * rotateSpeed * Time.deltaTime;
Camera.main.transform.Rotate(0, rotHorizontal, 0, Space.World);
you might need to change the axis in the rotation.
Hi. Not exactly that but similar. I am a bit confused now because what i want to do is similar to a state machine. rotate, wait, rotate back to original pos, and repeat this process. I might consider using the animation