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);
}
}