[resolved]problems with rotation

problems with rotation

I have two zoom and zoom back conditions,
Zoom returns to its initial position and rotation, but when I set a negative value, the camera starts to rotate, but when it sets a positive value, it remains normal. What can it be?

    public void Zoom() {
        StartCoroutine(MovOBJ(PosicMin, Speed));
        StartCoroutine(RotateOBJ(RotMin, Speed));
    }

    IEnumerator MovOBJ(Vector3 FinalPOS, float movSpeed) {
        Vector3 IniPOS = transform.position;
        for(float TempS = 0f; TempS <1f; TempS += movSpeed * Time.deltaTime)
        {
            transform.position = Vector3.Lerp(IniPOS, FinalPOS, TempS);
            yield return null;
        }

        transform.position = FinalPOS;
    }

    IEnumerator RotateOBJ(Vector3 FinalPOS, float RotSpeed) {
        Vector3 IniPOS = transform.rotation.eulerAngles;
        for(float TempS = 0f; TempS <1f; TempS += RotSpeed * Time.deltaTime)
        {
            transform.eulerAngles = Vector3.Lerp(IniPOS, FinalPOS, TempS);
            yield return null;
        }

        transform.eulerAngles = FinalPOS;
    }

    public void ZoomReturn()
    {
        StartCoroutine(MovOBJ(PosicMax, Speed));

        StartCoroutine(RotateOBJ(RotMax, Speed));
    }

    IEnumerator MovOBJ2(Vector3 FinalPOS, float movSpeed) {
        Vector3 IniPOS = transform.position;
        for(float TempS = 0f; TempS <1f; TempS += movSpeed* Time.deltaTime)
        {
            transform.position = Vector3.Lerp(IniPOS, FinalPOS, TempS);
            yield return null;
        }

        transform.position = FinalPOS;
    }

    IEnumerator RotateOBJ2(Vector3 FinalPOS, float RotSpeed) {
        Vector3 IniPOS = transform.rotation.eulerAngles;
        for(float TempS = 0f; TempS <1f; TempS += RotSpeed * Time.deltaTime)
        {
            transform.eulerAngles = Vector3.Lerp(IniPOS, FinalPOS, TempS);
            yield return null;
        }

        transform.eulerAngles = FinalPOS;
    }
}

Hello, Euler angles, my old nemesis.

Short answer: Euler angles are bad, and you should avoid using them except in very specific circumstances, otherwise it will lead to weird behavior like this.

Use transform.rotation and Quaternion.Slerp instead.

Friend, I don’t know if I got it right, but I adapted the script with your tip, but still the error continues, the script is now like this.would you have any ideas?

    public void Zoom() {
        StartCoroutine(MovOBJ(PosicMin, Speed));
        StartCoroutine(RotateOBJ(RotMin, Speed));
    }

    IEnumerator MovOBJ(Vector3 FinalPOS, float movtSpeed) {
        Vector3 IniPOS = transform.position;
        for(float TempS = 0f; TempS <1f; TempS += movtSpeed * Time.deltaTime)
        {
            transform.position = Vector3.Lerp(IniPOS, FinalPOS, TempS);
            yield return null;
        }

        transform.position = FinalPOS;
    }

    IEnumerator RotateOBJ(Quaternion FinalPOS, float RotSpeed) {
        Quaternion IniPOS = transform.rotation;
        for(float TempS = 0f; TempS <1f; TempS += RotSpeed * Time.deltaTime)
        {
            transform.rotation = Quaternion.Slerp(IniPOS, FinalPOS, TempS);
            yield return null;
        }

        transform.rotation = FinalPOS;
    }

    public void ZoomReturn()
    {
        StartCoroutine(MovOBJ2(PosicMax, Speed));

        StartCoroutine(RotateOBJ2(RotMax, Speed));
    }

    IEnumerator MovOBJ2(Vector3 FinalPOS, float movSpeed) {
        Vector3 IniPOS = transform.position;
        for(float TempS = 0f; TempS <1f; TempS += movSpeed* Time.deltaTime)
        {
            transform.position = Vector3.Lerp(IniPOS, FinalPOS, TempS);
            yield return null;
        }

        transform.position = FinalPOS;
    }

    IEnumerator RotateOBJ2(Quaternion FinalPOS, float RotSpeed) {
        Quaternion IniPOS = transform.rotation;
        for(float TempS = 0f; TempS <1f; TempS += RotSpeed * Time.deltaTime)
        {
            transform.rotation = Quaternion.Slerp(IniPOS, FinalPOS, TempS);
            yield return null;
        }

        transform.rotation = FinalPOS;
    }

Hm… that’s strange. It looks right to me. It still has the same behavior as before? No compile errors?

Might be beneficial to see the whole script.

This isn’t your issue, but just to make the code simpler to follow and understand, it looks like your two pairs of coroutines are identical to each other in code. Might consider removing MoveOBJ2 and RotateOBJ2 and just using the other functions for both zooming in and out. You could also combine the movement and rotation into a single coroutine that uses the same for loop, if you want.

this happens, I enter a value in the variable and it sets another totally different
the script has nothing more, to add there are not only the variables that are these

    public Quaternion RotMin, RotMax;
    public Vector3 PosicMin, PosicMax;

https://www.youtube.com/watch?v=bPVzeqpu7EM

Alright, knowing that you’re entering quaternion values into the inspector is important. At this point, read the Euler angle article I linked above, which should help you understand why the values you’re typing into that quaternion aren’t working. It’ll also give you the logic to understand the main exception to the “don’t use Euler angles” rule, which is: Euler angles are okay when a human needs to type in rotation values.

So, use Euler angles for the value you type into the inspector, then immediately convert that into a Quaternion and never use the Euler angles for anything else.

1 Like

now I understand, what a boring thing this quaternion is, thanks for the information, resolved