简体   繁体   中英

why does the orientation of my object change in unity?

I have a model in Unity which i want to rotate using a Quaternion given by an IMU. To change the orientation i need to set the object's initial orientation Quaternion(=Qreset) to the orientation of the IMU (=Qimu) and from then on calculate the difference between Qreset and Qimu to get the new orientation. It works, however whenever i set the initial orientation (Qreset) the orientation of the object in Unity always changes (to X: -180.4, Y: 21.08, Z: 21.08). I don't know why this happens. How do i change this so object orientation at reset is always 0,0,0. This is my code:

void Update() {

if (Input.GetKeyDown(KeyCode.Space))
   {
      Qreset = Qimu;
   }
   transform.rotation = Qreset * Quaternion.inverse(Qimu);
}

void Splitstring(string msg)
{
    string[] values = msg.Split('+');    
    Qimu.x = float.Parse(values[1]);
    Qimu.y = float.Parse(values[2]);
    Qimu.z = float.Parse(values[3]);
    Qimu.w = float.Parse(values[0]);
}

There is a difference between rotation

The rotation of the transform in world space stored as a Quaternion.

and localRotation

The rotation of the transform relative to the transform rotation of the parent.

And note that in the Transform Inspector

The position, rotation and scale values of a Transform are measured relative to the Transform's parent. If the Transform has no parent, the properties are measured in world space.

=> what you see in the Inspector is usually the localRotation

So if your object is nested under the Qimu you want to set

transform.localRotation = Qreset * Quaternion.inverse(Qimu);

Also note the Draco18s' comment about retrieving the Quaternion. You rather should do

void Splitstring(string msg)
{
    string[] values = msg.Split('+');    
    var x = float.Parse(values[1]);
    var y = float.Parse(values[2]);
    var z = float.Parse(values[3]);
    var w = float.Parse(values[0]);

    Qimu = new Quaternion(x, y, z, w);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM