Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Instantiating from an AssetBundle Rotation inconsistency

Discussion in 'Physics' started by MarkvDrimmelen, Feb 10, 2018.

  1. MarkvDrimmelen

    MarkvDrimmelen

    Joined:
    Oct 13, 2017
    Posts:
    13
    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.
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class SpawnCubes : MonoBehaviour
    5. {
    6.     public GameObject Cube;
    7.  
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         var assetBundle = AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundles/spawnables");
    12.         GameObject cubeFromAB = assetBundle.LoadAsset<GameObject>("assets/prefabs/cube.prefab");
    13.  
    14.         GameObject cube1 = Instantiate(Cube, new Vector3(-4f, 0.5f, 0f), Quaternion.Euler(0, 180, 0));
    15.         GameObject cube2 = Instantiate(Cube, new Vector3(-2f, 0.5f, 0f), default(Quaternion));
    16.         GameObject cube3 = Instantiate(cubeFromAB);
    17.         GameObject cube4 = Instantiate(cubeFromAB, new Vector3(2f, 0.5f, 0f), default(Quaternion));
    18.         GameObject cube5 = Instantiate(cubeFromAB);
    19.  
    20.         StartCoroutine(moveQube(cube5, new Vector3(4f, 0.5f, 0f), Quaternion.Euler(0, 180, 0)));
    21.     }
    22.  
    23.     IEnumerator moveQube(GameObject cube, Vector3 position, Quaternion rotation)
    24.     {
    25.         yield return new WaitForSeconds(2);
    26.  
    27.         cube.transform.SetPositionAndRotation(position, rotation);
    28.     }
    29. }
    30.  
    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
     

    Attached Files:

    Last edited: Feb 10, 2018
    Leniaal likes this.
  2. MarkvDrimmelen

    MarkvDrimmelen

    Joined:
    Oct 13, 2017
    Posts:
    13
    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.
     
  3. isgoed

    isgoed

    Joined:
    Oct 18, 2019
    Posts:
    4
    Have you find a solution/workaround? (I am on Unity 2019.2.5f1)

    I tried forcing to update the correct rotation but no effect:
    Code (CSharp):
    1. //loaded_asset.transform.rotation.Normalize();
    2.             Vector3 Rotation = loaded_asset.transform.rotation.eulerAngles;
    3.             Rotation.x %= 360;
    4.             Rotation.y %= 360;
    5.             Rotation.z %= 360;
    6.             loaded_asset.transform.rotation = new Quaternion();
    7.             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.
    2020-02-27-Loaded-Prefab2.png
     
  4. isgoed

    isgoed

    Joined:
    Oct 18, 2019
    Posts:
    4
    Even when I print the quaternion values they show to be normalized:
    2020-02-27-Loaded-Prefab6.png
     
  5. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    709
    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.
     
    Last edited: Jan 22, 2023
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
  7. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    709
    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!
     
    MelvMay likes this.