1
votes

I am trying to rotate a camera around an object but stop when the angle hits 90 degrees in the y-axis.

I have found options like "RotateTowards": https://docs.unity3d.com/ScriptReference/Quaternion.RotateTowards.html, and options like "RotateAround": https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html, but I am trying to find a way to combine both.

Code I have tried but that hasn't successfully worked:

void Update()
{
    if (cameraChange && transform.rotation.y != 90)
    {
        // transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, 90f, 0), Time.deltaTime * speed);
           transform.rotation.RotateAround(player.transform.position, new Vector3(0, 1, 0), Time.deltaTime * speed);

    }
}

void GameOver()
{
    cameraChange = true;
    transform.LookAt(player.transform);
}
1

1 Answers

0
votes

The problem you're having is transform.rotation returns a quaternion, the y component of a quaternion does not correspond to the y-axis, in fact, it's advised you don't touch those values directly at all unless you're really very familiar with quaternions.

Try:

transform.rotation.eulerAngles.y != 90 // this will correspond to the y-axis

Assuming your code to do the rotation works, this should fix your issue