1
\$\begingroup\$

I have a camera in my scene that I want to rotate around the player. The rotation is done when the camera rotation state is at CameraRotation.Left or CameraRotation.Right and CameraRotation.None indicates that the camera should stop rotating and just follow the player.

I want the camera to rotate around the player and stop after it reaches a specific angle (180 degrees) in the y axis in the CameraRotation.Left or CameraRotation.Right from the position the camera was at the start of the rotation. It would also be helpful if some direction is given on how to edit the SmoothFollow() method so that it follows the player with the camera still rotated.

The problem is I'm not familiar with rotations and not sure whether to use Euler angles or quaternions so any tips would help.

private void LateUpdate()
{
    if (GameManager._RotateCamera == CameraRotation.None)
    {
        SmoothFollow();
    }
    else if (GameManager._RotateCamera == CameraRotation.Left)
    {
        transform.RotateAround(player.transform.position, Vector3.up, rotationSpeed * Time.deltaTime);
    }
    else if (GameManager._RotateCamera == CameraRotation.Right)
    {
        transform.RotateAround(player.transform.position, Vector3.down, rotationSpeed * Time.deltaTime);
    }
}

// Make the Camera follow the player smoothly
private void SmoothFollow()
{
    Vector3 desiredPoistion = player.position + offset;
    Vector3 smoothedPosition = Vector3.SmoothDamp(transform.position, desiredPoistion, ref velocity, smoothSpeed);

    transform.position = smoothedPosition;

    transform.LookAt(player.transform);
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Can you explain what you mean by "I want the camera to rotate around the player and stop after it reaches a specific angle (180 degrees) in the y axis."? 180 degrees from what? Do you mean to stop after RotateAround exactly 180 degrees in the time after some function has been called? \$\endgroup\$
    – Ruzihm
    Commented Dec 18, 2018 at 19:25
  • \$\begingroup\$ Yes, that is exactly what I mean I want the camera to stop rotating around the player.transform.position after it rotates 180 degrees and I'm not sure how to exactly do that because I want it to stop on its own without any player input. \$\endgroup\$ Commented Dec 19, 2018 at 6:30

1 Answer 1

0
\$\begingroup\$

You need to add a variable to keep track of how much rotation has already occurred.

private float cumulativeRotation = 0f;

When you start rotating, make sure that it is set it to zero if it isn't already:

cumulativeRotation = 0f;

Then, in your update, compare how much rotation is left (180f - cumulativeRotation) vs. how much rotation you can do based on the rotation speed (rotationSpeed * Time.deltaTime) and do the lesser one. Whatever you choose, add that value to the cumulativeRotation.

Then, if cumulativeRotation >= 180f ( I like to avoid using == between floats), you can end rotation. I assume here that we can do that by just using GameManager._RotateCamera = CameraRotation.None); but you may have a better way. Optionally, reset the cumulativeRotation tracker to 0f once rotation is complete.

private void LateUpdate()
{
    if (GameManager._RotateCamera == CameraRotation.None)
    {
        SmoothFollow();
    }
    else {
        float rotationThisFrame = Mathf.Min(
                180f - cumulativeRotation, 
                rotationSpeed * Time.deltaTime);
        if (GameManager._RotateCamera == CameraRotation.Left)
        {
            transform.RotateAround(player.transform.position, Vector3.up,
                    rotationThisFrame );
        }
        else if (GameManager._RotateCamera == CameraRotation.Right)
        {
            transform.RotateAround(player.transform.position, Vector3.down,
                    rotationThisFrame );
        }

        cumulativeRotation += rotationThisFrame;

        if (cumulativeRotation >= 180f) {
            // We are done rotating. Stop rotating on further frames.
            GameManager._RotateCamera = CameraRotation.None;

            // Reset cumulativeRotation tracker for next time.
            cumulativeRotation = 0f;
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Thank you that is exactly what I was looking for. I wonder though why do we compare the rotation left and rotation speed, instead of just using the rotation speed? \$\endgroup\$ Commented Dec 19, 2018 at 8:18
  • \$\begingroup\$ @dragonSlayer Suppose rotationSpeed * Time.deltaTime is 13f and cumulativeRotation is 169f. Then, you actually don't want to turn 13 degrees this frame, because that would put you at 182 degrees from the start. You would only want to turn 11 degrees this frame. \$\endgroup\$
    – Ruzihm
    Commented Dec 19, 2018 at 8:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .