How to make the smallest possible rotation?

Hello, I’m trying to make my object rotate to spin (0, transform.localEulerAngles.y, 0) via the shortest path possible, and for this I am using this code snippet below:

void FixedUpdate () {
Vector3 paraOndeRodar = new Vector3((-transform.eulerAngles.x+360),transform.eulerAngles.y,(-transform.eulerAngles.z+360));
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler(paraOndeRodar), Time.deltaTime);
}

But I’m having trouble because the unity converts angles greater than 90 automatically, so if the objeti is upside down, it stabilizes anyway, and this is not what I want.

Someone would know an efficient way to make the object turn to (0, rotacY, 0) at the lowest possible way?

If possible, AddTorque

(I apologize for my bad English)


I tried to stabilize the code below , but failed.

using UnityEngine;
using System.Collections;

public class Estabiliz : MonoBehaviour {
    void FixedUpdate () {
        Vector3 paraOndeRodar = new Vector3((-transform.eulerAngles.x+360),transform.eulerAngles.y,(-transform.eulerAngles.z+360));
        Quaternion rotacFinal = Quaternion.Euler (paraOndeRodar);
        if (Mathf.Abs (rotacFinal [0]) > 0.7f) {
            Vector3 rotacINV = rotacFinal.ToEulerAngles ();
            Vector3 novaRotacINV = new Vector3 (rotacINV.x + 180, rotacINV.y, rotacINV.z);
            rotacFinal = Quaternion.Euler (novaRotacINV);
        }
        if (Mathf.Abs (rotacFinal [2]) > 0.7f) {
            Vector3 rotacINV = rotacFinal.ToEulerAngles ();
            Vector3 novaRotacINV = new Vector3 (rotacINV.x, rotacINV.y, rotacINV.z + 180);
            rotacFinal = Quaternion.Euler (novaRotacINV);
        }
        transform.rotation = Quaternion.Slerp (transform.rotation, rotacFinal, Time.deltaTime);
    }
}

You want to… spin around the Y axis as much as you’re already spun around the y-axis?

That means that if your current y-angle is 0, you won’t spin, and if your current y-angle is 60, you’ll spin to 120. What exactly are you trying to do?

You’re also indexing the quaternion directly with [0] and [2]- that’ll give you the x-component of the quaternion, which is NOT the same as the x-angle or z-angle of the euler angles. Are you sure that’s what you’re looking for?

You should also seriously consider starting to write code in English. When you’re using your native language, the vast majority of the people you’ll ask for help online won’t be able to understand what your code is trying to do. It’s better to write code in English that’s a bit broken than to write it in Spanish, as most of the community simply won’t be able to read your Spanish.

I’m not a native English speaker myself, so don’t take this as cultural imperialism. It’s just good practice.

void FixedUpdate(){
        transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.LookRotation (Vector3.Cross (transform.right, Vector3.up).normalized), Time.deltaTime);
    }

My intention is to do something in the style of code that just posting, but without the problem of reversing the rotation when the axes pass from + - 90

with this code I just post the intention is that the Y axis is free, and the X and Z axis are normalized, and this is good. But if you turn the Z axis more than 90, the Y axis will automatically be rotated because of the angle conversions, and I do not want this to happen.