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

Bug Problem with scaled objects in Unity AR Foundation

Discussion in 'AR' started by DaRaccoon, Nov 7, 2022.

  1. DaRaccoon

    DaRaccoon

    Joined:
    Oct 28, 2018
    Posts:
    11
    I'm trying to build a little shooter game in AR and i've encountered a bug that i don't understand very well...
    but first i need to go through some code i made to give you the full picture of the functionalities:
    Basically my app finds an Horizontal plane, spawns a block in the centre of it and add them all in a struct that has plane + associated block and put it in a list of structs (don't worry about this struct, it's not the main issue here and this happened even when i was not using it)

    Code (CSharp):
    1.  public void PlaneUpdated(ARPlanesChangedEventArgs args)
    2.         {      
    3.             if (args.added != null && planes.Count < maxObjects)
    4.             {          
    5.                 ARPlane arPlane = args.added[0];
    6.                 PlaneObj temp = new PlaneObj(arPlane, (Instantiate(PlaceablePrefab, arPlane.transform.position, Quaternion.identity))); //the struct element
    7.                 planes.Add(temp);    //planes = lists of structs    
    8.             }  
    9.         }

    then, when five planes have been detected and 5 blocks are placed, a button appears which, when pressed, calls for the UpdateSize function and disappears. the function is the following:


    Code (CSharp):
    1. public void UpdateSize()
    2.         {
    3.             lookPoint.transform.position = Camera.main.transform.position;      
    4.             this.GetComponent<UpdateObjects>().updateObjects(planes, lookPoint); //changed to lookPoint    
    5.         }      
    6.  
    lookpoint is a phisical object with a tag and a cube collider which is used as an anchor for enemy movement and lookAT functions, because it was easier to just have a fixed gameObject on the scene instead of using the actual camera that constantly moves around.
    The function then calls for updateObjects, which takes every item in the "planes" list of structs, takes the scaling for x and y of the plane and adds them to the scale of the object associated with the current plane. After that, it invokes the LookAt function rotating the cubes towards lookPoint. Then, it starts spawning enemies.

    Code (CSharp):
    1. public void updateObjects(List<PlaceObjectsAutomated.PlaneObj> planes , GameObject lookPoint)
    2.         {              
    3.             foreach(PlaceObjectsAutomated.PlaneObj planeObj in planes)
    4.             {
    5.                 var scaling = new Vector3(planeObj.plane.size.x * 2, planeObj.plane.size.y * 2, 0); //planeObj.obj.transform.localScale.z);
    6.                 planeObj.obj.transform.localScale += scaling;
    7.                 planeObj.obj.transform.LookAt(lookPoint.transform.position);        
    8.             }
    9.             this.GetComponent<SpawnEnemy>().spawnEnemy(planes, lookPoint);
    10.         }
    11.  

    here lies the problem: Once the updateObjects function is called, all the blocks appears to be scaled by a certain amount (but i'd like a suggestion to try and make them as big as the plane they are in) and appears to look correctly at the lookPoint...but then, after i move the camera a little they all immediately look in random directions and morph into strange shapes which are not rectangles or cubes anymore, messing up a lot of the rest of the codes... i really don't know why, since the update is called only once and at first it all works correctly before breaking... care to help me a little?
     
  2. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,076
  3. DaRaccoon

    DaRaccoon

    Joined:
    Oct 28, 2018
    Posts:
    11
    i've seen this thread but it talkes about scaling a scene, i need to chenge the size of objects according to the plane they are on, if i'd scale the session origin every cube would be scaled the same, and also, quoting the thread, "For simple content, like a cube, it's probably much easier just to scale the content" so i don't think it fixes the problem i'm having
     
    KyryloKuzyk likes this.
  4. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    818
    It sounds like the root of your problem is that something is changing the scale and rotation of objects in your scene at runtime, and you don't know what. I would recommend setting breakpoints in your code and stepping through the update loop to see what is causing your problem. More info about debugging here: https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

    If you're not sure where to start, it might help to create a new scene where you add your components back in one at a time, verifying that everything works as expected after you've added each component. This might help you isolate where the source of your problem is.

    Your current scaling algorithm is asymmetric: you are scaling objects differently on their X and Y components, and not at all on their Z components, by adding a vector to the local scale. This could cause visual artifacts like what you are describing where the shape of the object is no longer intact:

    Code (CSharp):
    1. planeObj.obj.transform.localScale += scaling;
    Instead of adding the plane size to your scale, you might try uniformly multiplying all components of your scale vector by some factor of the plane size, ie:
    transform.localScale *= (plane.size.x + plane.size.y) / 2


    In this example we uniformly scale your object by a factor of the average side length of the plane, as one idea. (You might need to fiddle with the numbers more to get a desirable result in your project.)

    Anyway hope that helps!
     
    KyryloKuzyk and DaRaccoon like this.
  5. DaRaccoon

    DaRaccoon

    Joined:
    Oct 28, 2018
    Posts:
    11

    Hi, sorry if it took me some time and i hope you'll get this since it appears your quite knowledgeable about ar! I found the exact problem: it wasn't a scaling problem, it was the function LookAt! Basically the prefabs i place on the plane snap right back in their default rotation after some time has passed... do you know why perhaps? do you think is a problem in my code or it's a problem between AR and the LookAt transform function?
     
  6. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    818
    Unfortunately I can't help you debug your code, but you might be able to simplify the problem by testing a simple 3d scene without AR. LookAt works the same in a regular 3D scene as it does in AR.
     
  7. angelsm85

    angelsm85

    Joined:
    Oct 27, 2015
    Posts:
    62
    I have a similar problem. When I instantiate an object in an AR scene it appears with the correct scale but when I switch to another AR scene and instantiate the same object the object scale changes on the screen. It gets smaller. And if I load another screen it appears smaller.... ARSessionOrigin scale is set to (1,1,1). Can anyone help me to find a solution?
     
    Last edited: Nov 16, 2022
  8. DaRaccoon

    DaRaccoon

    Joined:
    Oct 28, 2018
    Posts:
    11
    there was no need to debug the code, litterally the whole simple code that manages block scaling and rotation is written in this thread, the cubes i spawn don't do anything besides these 24 lines of code

    Code (CSharp):
    1.  planeObj.obj.transform.LookAt(lookPoint.transform.position);
    this is litterally the only place the function lookAT is invoked for this game object, there is no further code that change rotation or scaling, but the cube go back to the default rotation by its own, and not only the cube, but EVERY object placed on the scene return to the default spawn rotation, no matter if a write further code or not
     
  9. DaRaccoon

    DaRaccoon

    Joined:
    Oct 28, 2018
    Posts:
    11

    and i'm starting to think that it's a bug that comes with AR, because the initial problem with the scaling was due to the fact that while some parts of the cube rotated back to the initial form, the rest of the cube that was scaled with the function didn't, creating those strange shapes

    I already have a dummy 3d project in which i test every function, but this doesn't happen in that project since it doesn't have the same variables, i'm using AR planes that come with a lot of different problems and solution that i cannot try in a 3d environment.
     
    Last edited: Nov 18, 2022