Search Unity

How to use local scale?

Discussion in 'Scripting' started by SoftwareGeezers, Oct 1, 2014.

  1. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    I'm trying to scale a beam according to a ray length and object hit, but it's getting messed up depending on the scale of the parent objects.

    Ship (local scale 1,1,1)
    ...|-- Turret (local scale 1,1,1)
    .........|-- Beam (local scale 0.15, 5)

    I build this runtime so add a turret and beam to Ship. If I look at the objects in the Inspector at run time, the shown scales are as above. If I print the localScales in Start() though, they are different. If the root ship is scaled (2,2,1), the beam localScale as printed is (0.2,5,1). If the root ship is scaled (1,1,1), the beam localScale as printed is (0.3,10,1). And if I set the beam's local scale to the length of a ray, it's either too long on a large ship or too small on a small ship depending how I scale it according to scale factor.

    My mesh is a 1 length square imported from Collada with a scale of 1, so that shouldn't be affecting anything.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    First: transform.localScale will always have the same value as the numbers that are displayed in the inspector.

    Depends on how you're changing the scales of these objects. Most notably: are the objects in that hierarchy the entire time? If the Beam is not a child of the Ship when the Ship' scale changes, for example, it will be a different localScale when it is re-attached.

    Also, when you do Debug.Log("Scale is "+transform.localScale); the Vector3's default ToString function truncates the values to the nearest tenth. If you want to get the more detailed "real" numbers, use Debug.Log("Scale is ("+transform.localScale.x+", "+transform.localScale.y+", "+transform.localScale.z+")");

    Finally, although having a mesh on these objects wouldn't change anything, an Animator certainly could. If you have one of those, disable it while you're experimenting with this stuff.
     
  3. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    No animator. And I'm definitely getting different numbers in the inspector versus the print statement. Just trying again this very second, outpost from
    ...is...
    ...while the inspector shows the beam local scale as (0.15, 5, 1.0) and the parent object's local scale as (1,1,1). Root object's scale is also (1,1,1)
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Well.... that's.... uh....


    Hm... I have no explanation. Could you post a small reproducible test case project file of this?
     
  5. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    Ahh, thanks for the info. Got me looking closer and I was being stupid. The thing I was looking at isn't the thing being scaled, but the 'master' shot cloned and placed. I've been working so long at this game that I've forgotten how half of it works! :confused:
     
  6. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    Final update and reference for future users. Unity automagically handles local scale when adding objects so they scale to original size. A prefab of scale (1,5,1) added to an object of local scale (0.5, 0.5, 1) is given a local scale of (2,10,1) to keep the original size. When scaling local scale, you have to factor in the scales of the whole hierarchy of transforms. In my case, I multiply the wanted beam length from the ray-cast (absolute space values) by the aggregate inverse scale :
    Code (csharp):
    1. shipScale = 1/transform.parent.localScale.x/transform.root.localScale.x;
    2.  
    3. vec2.x = beamWidth;
    4. vec2.y = beamLength * shipScale;
    5. transform.localScale = vec2;
    6.  
     
    xexuxjy likes this.