Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
3
Question by Nanako · Jul 15, 2015 at 02:28 PM · transformglobalspace

Reading and Setting an object's global scale, with transform functions?

Hi all. this seems to be a complex problem.

I need to be able to read, and also set, the global scale of an object, which is a child many layers deep in a hierarchy. The layers above it may have any arbitrary scales, rotations and positions Apparently this is done using transform matrices, but i'm not aware of how to use them.

It can also apparently be done using the Transform.Transform* functions. But there are three of those, and I'm not sure which to use TRansformPoint TransformVector TransformDirection

Accuracy is more critical than performance here. I need a solution that works, even if slowly.

Comment
Add comment · Show 1
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 Nanako · Jul 17, 2015 at 02:54 AM 0
Share

Ok in addition i notice that, if i remove a skewed child object from its parent, the mesh snaps back to conform to the unskewed collider, and the roitation and scale of the object correct themselves to keep it in the same point in worldspace.

So whatever i need to be done is possible, and unity already does it in this circumstance. I just need to know how to replicate exactly what unity does here.

I do not need the skew values

alt text

2 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by Seneral · Apr 21, 2017 at 02:45 PM

The programmer way to do this:

 public static void SetGlobalScale (this Transform transform, Vector3 globalScale)
 {
     transform.localScale = Vector3.one;
     transform.localScale = new Vector3 (globalScale.x/transform.lossyScale.x, globalScale.y/transform.lossyScale.y, globalScale.z/transform.lossyScale.z);
 }
Comment
Add comment · Show 2 · 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 Obsdark · Aug 01, 2017 at 03:35 AM 0
Share

Great Simple function than actualy works like a charm!, it would be cool put some details about what it does about, hopefully they'll add it to the main program some day ;)

avatar image Kesarium · Aug 13, 2020 at 12:37 PM 0
Share

I spent a whole day trying to achieve this result and failed until I found this answer. It works like magic! Thank you.

avatar image
3

Answer by Owen-Reynolds · Jul 15, 2015 at 04:15 PM

A cute trick is just to unparent the object, set the scale (which is still the localScale variable, but which is really global scale,) then reparent it. Unity will recompute the correct localScale (with respect to parent rotations and scales) for you.

If the parent rotations are such that you have non-uniform scaling problems, then it can't be done. Not by you or Unity. In that case, "fake" it having a parent by having code track the position/rotation of a pretend parent.

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 Nanako · Jul 17, 2015 at 02:18 AM 0
Share

That's a pretty neat idea, i'll keep that in $$anonymous$$d.

Regarding the non uniform scaling thing, i understand that is not possible for rendered meshes, but i did some testing and noticed something curious

![alt text][1]

On the right there is one object with a box collider (parented to the big thing on the left)

The collider is outlined in green, and you may notice that although the object is skewed, its collider is not. The collider holds a firm and rigid box shape.

The collider is what i want to know the size of. If it's immune to skew then perhaps it can be done? [1]: http://i.imgur.com/FIZYfSy.png

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

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

CharacterControllers and Transform.Rotate 1 Answer

Can't move instantiated prefab 2 Answers

aiming up and down 0 Answers

get bone path from transform 1 Answer

Need enemy to follow only active character 0 Answers


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