Instantiating from an AssetBundle Rotation inconsistency

Hello,

I am encountering an issue when instantiating GameObjects from an AssetBundle where (about half of the time) the object is instantiated with random Rotation values (i.e. X:-7.730941e+11, Y:-7.730941e+11, Z:180) instead of the given rotation. In contrast, performing the same instantiating while having a GameObject reference to the prefab inside the script seems to be accurate 100% of the time.

To illustrate the problem I have attachted a demo project that tests the following scenario’s:

  • Instantiate GO reference cube with different position and rotation.
  • Instantiate GO reference cube with different position.
  • Instantiate GO from AssetBundle with default values.
  • Instantiate GO from AssetBundle with different position.
  • Instantiate GO from AssetBundle with default value and changes position and rotation after 2 seconds.
using System.Collections;
using UnityEngine;

public class SpawnCubes : MonoBehaviour
{
    public GameObject Cube;

    // Use this for initialization
    void Start()
    {
        var assetBundle = AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundles/spawnables");
        GameObject cubeFromAB = assetBundle.LoadAsset<GameObject>("assets/prefabs/cube.prefab");

        GameObject cube1 = Instantiate(Cube, new Vector3(-4f, 0.5f, 0f), Quaternion.Euler(0, 180, 0));
        GameObject cube2 = Instantiate(Cube, new Vector3(-2f, 0.5f, 0f), default(Quaternion));
        GameObject cube3 = Instantiate(cubeFromAB);
        GameObject cube4 = Instantiate(cubeFromAB, new Vector3(2f, 0.5f, 0f), default(Quaternion));
        GameObject cube5 = Instantiate(cubeFromAB);

        StartCoroutine(moveQube(cube5, new Vector3(4f, 0.5f, 0f), Quaternion.Euler(0, 180, 0)));
    }

    IEnumerator moveQube(GameObject cube, Vector3 position, Quaternion rotation)
    {
        yield return new WaitForSeconds(2);

        cube.transform.SetPositionAndRotation(position, rotation);
    }
}

Executing this code multiple times keeps comming back with different results where sometimes the results are accurate, and sometimes random Rotation values are inserted. Another strange this is that, although random Rotation values are inserted, the objects in the editor seem to be accurate in world space.

So I was wondering, has anyone experienced similar issues, and am I perhaps doing something wrong while loading the prefab from the asset bundle?

Thanks in advance

3387029–266149–AssetBundleTransformTest.rar (1.29 MB)

1 Like

Update: It appears to be an editor bug. I just figured I would print the actual rotation of the objects and it turns out those are in fact accurate, in contrast to the random values displayed in the editor.

Have you find a solution/workaround? (I am on Unity 2019.2.5f1)

I tried forcing to update the correct rotation but no effect:

//loaded_asset.transform.rotation.Normalize();
            Vector3 Rotation = loaded_asset.transform.rotation.eulerAngles;
            Rotation.x %= 360;
            Rotation.y %= 360;
            Rotation.z %= 360;
            loaded_asset.transform.rotation = new Quaternion();
            loaded_asset.transform.rotation = Quaternion.Euler(Rotation);

This only happens for loaded assetbundles (prefabs?), but not for anything already in the scene.

See example: the rotation should be (0,0,0). When I print the rotation Euler angels, I indeed get: 0,0,0.
5528254--567991--2020-02-27-Loaded-Prefab2.png

Even when I print the quaternion values they show to be normalized:
5528275--567994--2020-02-27-Loaded-Prefab6.png

This appears to be causing RotationConstraint to break, since RotationConstraint relies on Euler angles for the offsets.

In Unity editor I see the rotation on the Prefab as:
x=171.67 y=0.614 z=0.089

While after instantiating the assetbundle the same transform’s rotation shows as:
x=-7.730941e+11 y=975960.6 z=-7.730941e+11

I am finding assetbundles with RotationConstraint will often initialize incorrectly due to the “AssetBundle Rotation inconsistency” causing the RotationConstraint to rotate incorrectly and makes the asset appear distorted, the original prefab works perfectly, but assetbundles are very unstable.

Ignoring the necro, note that I don’t believe this thread is related to the RotationConstraint which itself isn’t related to physics at all; it’s an animation thing and doesn’t relate to PhysX (3D physics).

Better forums for this info would be here (or a bug report for those teams):

Thanks, sorry for posting a constraints question in the physics forum, I only glanced at the question. But I was able to solve the issue pretty reliably by adding a component and storing metadata about the euler angles for each gameobject with RotationConstraint component. Then after loading the assetbundle search for each component and apply the stored metadata values to the constraint. Probably not necessary for Quaternions but since constraints rely on Euler angles I needed to use the same exact values from the original prefab. Not ideal but was simple to solve!

1 Like