Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ikeo · Sep 14, 2010 at 05:06 PM · transformscalehierarchyglobal

How do I set the global scale of an object in a transform hierachy?

I've written a COLLADA importer that uses the element of the COLLADA file to define rotation, translation, and scale values for transforms. The values of the matrix are as follows:

xx,xy,xz,t1,yx,yy,yz,t2,zx,zy,zz,t3,0,0,0,1

Rotation and translation are working correctly by creating the coordinate basis from the x,y, and z axes, and taking the x,y, and z position values. The problem is that scale values, which are derived from the magnitude of the basis vectors represent scaling in the global coordinate system (not the local system as one would assume). This is a problem because after creating the transform, parenting it, and setting it's translation and rotation, the local system is rotated and setting transform.localScale (the only scale property available) applies the global scale values to the local system.

How do I set a transform's global scale in a transform hierarchy?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image skovacs1 · Sep 14, 2010 at 09:17 PM 0
Share

These are naive brute force approaches: Could you not just traverse the hierarchy and compute local scale relative to the parents? Even more simply, could you not store the parent, detach from it, scale and then re-attach to it?

avatar image Paulius-Liekis · Sep 15, 2010 at 02:05 PM 0
Share

Do you mean you're writing your own COLLADA-to-Unity importer? Why? Unity can import COLLADA files already...

avatar image ikeo · Sep 16, 2010 at 12:53 AM 0
Share

It's part of my app that downloads 3D models and builds them in Unity at run-time. So I needed a script to parse COLLADA models. Also, my script reads extra information inside the COLLADA.

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by Alex 17 · Apr 18, 2011 at 10:30 AM

Maybe it will be helpful:

function GetParentScale(): Vector3 { var sf: Vector3 = Vector3.one;

 var parentTransform: Transform = null;
 if (thisTransform.parent) parentTransform = thisTransform.parent;

 while (parentTransform) {
     sf.x *= 1.0f / parentTransform.localScale.x;
     sf.y *= 1.0f / parentTransform.localScale.y;
     sf.z *= 1.0f / parentTransform.localScale.z;
     if (parentTransform.parent) {
         parentTransform = parentTransform.parent;
     } else {
         parentTransform = null;
     }
 }
 return sf;      

}

function SetScale(globalScale: Vector3) { var parentGlobalScale: Vector3 = GetParentScale(); thisTransform.localScale.x = globalScale.x parentGlobalScale.x; thisTransform.localScale.y = globalScale.y parentGlobalScale.y; thisTransform.localScale.z = globalScale.z * parentGlobalScale.z; }

where thisTransform = gameObject.transform

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image justinbowes · Mar 20, 2015 at 10:45 AM 0
Share

This is why .lossyScale exists -- in most cases, if any of the parents is rotated, this function will return the wrong result.

avatar image
0

Answer by ikeo · Sep 18, 2010 at 03:12 AM

It was much easier than I was making it out to be. The COLLADA matrices make complete sense if you decompose them and set the localPosition, localScale, and localRotation as follows:

Vector3 x = new Vector3((float)matrix[0], (float)matrix[4], (float)matrix[8]); Vector3 y = new Vector3((float)matrix[1], (float)matrix[5], (float)matrix[9]); Vector3 z = new Vector3((float)matrix[2], (float)matrix[6], (float)matrix[10]); float xScale = x.magnitude; float yScale = y.magnitude; float zScale = z.magnitude;

newParentTrans.localPosition = new Vector3((float)matrix[3] conversion, (float)matrix[7] conversion, (float)matrix[11] * conversion); newParentTrans.localScale = new Vector3(xScale, yScale, zScale);

Quaternion rotation = newParentTrans.rotation; rotation.SetLookRotation(z/zScale,y/yScale); newParentTrans.localRotation = rotation;

Because well-formed COLLADA files have one top-level element, you can rotate the entire scene by -90 deg around the x axis, if Z_UP is true.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Possible bug? Unity forces transform Y scale to 0 when reordering in hierarchy, disappears if you try to fix 0 Answers

How to scale an object along an arbitrary axis in global space? 1 Answer

Global rotation on child object ? 2 Answers

Global transform position and scale for gameObject 0 Answers

Scale of GameObject is reset when enable an attached Animator component 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges