N
N
Nikolai Trifonov2016-03-23 06:58:01
Unity
Nikolai Trifonov, 2016-03-23 06:58:01

How to rotate camera counterclockwise using quaternions and euler angles?

The Q and E buttons are assigned to rotate the camera by 90 and -90 degrees.
The camera on the stage consists of a holder and a camera inside it; is rigidly attached to the character, follows his movement (as in diablo, titan quest, grim dawn, etc) and revolves around him.
Turning clockwise (+90 degrees) works correctly and turns smoothly, but when I turn it counterclockwise (-90 degrees): the camera starts to jerk, or turns too sharply.

void Turning(){
    Quaternion quatertion = transform.rotation;
    Vector3 eulerAngle = new Vector3(0, quatertion.eulerAngles.y, 0);
    
    if(rotationAngle - eulerAngle.y < 0.2f){
      if(rotationAngle == 360){
                rotationAngle = 0;
      }
            eulerAngle.y = rotationAngle;
            isTurning = false;
    } else {
      eulerAngle.y = Mathf.Lerp(eulerAngle.y, rotationAngle, rotationStep * Time.deltaTime);
    }
    m_Compas.rotation = eulerAngle.y;
        quatertion.eulerAngles = eulerAngle;
    parentTran.rotation = quatertion;
  }

How to improve this code?
Upd.1:
Chthonic hulk that doesn't work
void Rotator(float val) {
        rotationAngle += val;
        Quaternion quatertion = transform.rotation;
        Vector3 eulerAngle = new Vector3(0, quatertion.eulerAngles.y, 0);
        if (rotationAngle > 0) {
            if (rotationAngle - eulerAngle.y < 0.2f) {
                if (rotationAngle == 360 || rotationAngle == -360) {
                    rotationAngle = 0;
                }
                eulerAngle.y = rotationAngle;
                isTurning = false;
            } else {
                eulerAngle.y = Mathf.Lerp(eulerAngle.y, rotationAngle, rotationStep * Time.deltaTime);
            }
        }
        else if (rotationAngle < 0) {
            rotationAngle = 360 + rotationAngle;
            if (rotationAngle - eulerAngle.y < 0.2f) {
                if (rotationAngle == 360 || rotationAngle == -360) {
                    rotationAngle = 0;
                }
                eulerAngle.y = rotationAngle;
                isTurning = false;
            } else {
                eulerAngle.y = Mathf.Lerp(Mathf.Abs(rotationAngle), eulerAngle.y, -rotationStep * Time.deltaTime);
            }
        }
        m_Compas.rotation = eulerAngle.y;
        quatertion.eulerAngles = eulerAngle;
        parentTran.rotation = quatertion;
    }

Current Solution:
void Camera_Rotation() {
        if (rotationAngle > 0) {
            transform.RotateAround(targetGO.transform.position, Vector3.up, -rotationAmount);
            rotationAngle -= rotationAmount;
        }
        if (rotationAngle < 0) {
            transform.RotateAround(targetGO.transform.position, Vector3.up, rotationAmount);
            rotationAngle += rotationAmount;
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2016-03-23
@fridriekh

Quaternion neededRotation = transform.rotation;
neededRotation *= Quaternion.Euler(0, 90, 0); // this add a 90 degrees Y rotation
transform.rotation = Quaternion.Slerp(transform.rotation, neededRotation , Time.deltaTime * damping);
I wrote very fast, so just the general meaning:
Remember the current turn.
Add the one you want to it.
And gradually, as you like, from the current to the desired one, at least immediately, at least through Lerp, rotate.
I hope it helps)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question