Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Slin · Mar 18, 2010 at 10:50 PM · transformscaleglobal

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

Hello, I want to scale a model globally. This means that I want to scale it a long a vector in world space. There is only localScale, this means that I have to transform my vector to the objects space before using it for scaling. What I do is this:

handler.obj.gameObject.transform.localScale += handler.obj.gameObject.transform.worldToLocalMatrix.MultiplyVector(scaledir)*2;

But for some reason it just doesnt work correctly. It would be great if anyone could tell me what I am doing wrong :)


New problem when trying to do it as proposed in the answer:

It works great as long as the parents are there. But as soon as I remove them, the scale will be wrong. This is how I create my parent objects which only have a transform component (and handler also a mesh and script actually):

handler_rotate = Instantiate(Resources.Load("object_handler_scale"), transform.position, Quaternion.identity);
transform.parent = handler_rotate.transform;
handler_scale = Instantiate(Resources.Load("object_handler_scale"), transform.position, Quaternion.identity);
handler_rotate.transform.parent = handler_scale.transform;
handler = Instantiate(Resources.Load("object_handler_main"), transform.position, Quaternion.identity);
handler_scale.transform.parent = handler.transform;

Then I change their position, angle or scale and afterwards remove them like this in the function of the object called "handler" in the code above:

obj.handler_rotate.transform.DetachChildren(); obj.handler_scale.transform.DetachChildren(); transform.DetachChildren();

Destroy(obj.handler_rotate); Destroy(obj.handler_scale); Destroy(gameObject);

After these last lines, the scale of the remaining object changes to something that looks as if the bigest scale part just gets applied to the models local scale for each axis, so that it again has its initial shape but is smaller or bigger and not streched anymore. Am I doing something wrong here? It works fine if I change only the scale or the rotation, delete the parents and create and do only one transformation again. Am I right to assume that a change of transform.eulerAngles also changes transform.localEulerAngles and transform.rotation and so on? Thanks and sorry for those probably stupid questions, but I just started using Unity yesterday...

Comment
Add comment · Show 2
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 runevision ♦♦ · Mar 22, 2010 at 09:40 AM 0
Share

I clarified my answer below.

avatar image Castle24 · Jan 06, 2020 at 07:18 AM 0
Share

I implement the below scale matrix in an arbitrary direction in shader: alt text

But it seems wired and means no sense. alt text

math-20200106.png (40.8 kB)
sharedscreenshot.jpg (331.3 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by runevision · Mar 19, 2010 at 10:39 AM

transform.localScale can only scale along the x, y, and z axis, not along any arbitrary axis. It's not a vector that specifies a diretion of scaling; it's a vector that specifies a scaling multiplier for the x, y, and z axis.

This means that no matter what localScale you use, you will never be able to scale an object along an arbitrary axis. If you want to do that, you have to use a parent object aligned such that its x, y, or z axis points along the axis you want to scale in.

So, to scale a GameObject G along an arbitrary axis:

  • Make a new GameObject P and rotate it such that its local x, y, or z axis is along the global axis you want to scale.
  • Make the new GameObject P the parent of the GameObject G that you actually wanted to scale. (G should automatically counter-rotate so it retains its original orientation, unaffected by the rotation of the parent P.)
  • Scale the parent GameObject P the way you want.

This order of doing things will work both in the editor and through scripting. The important part is to rotate P before you make it the parent, but scale it after you make the parent.

Note that you cannot delete the parent P after having performed these steps if you want to keep the correct scaling. If you want to keep the scaling, you need to keep P as a parent to G.

Comment
Add comment · Show 4 · 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 Slin · Mar 19, 2010 at 12:50 PM 0
Share

Thanks for your answer, I will definatly give it a try. But I wonder, what is wrong with the way I am currently trying to go, as this seems much more direct to me?

avatar image runevision ♦♦ · Mar 19, 2010 at 02:02 PM 0
Share

transform.localScale can only scale along the x, y, and z axis. It's not a vector that specifies a diretion of scaling; it's a vector that specifies a scaling multiplier for the x, y, and z axis. This doesn't change no matter how you calculate the vector you assign to localScale.

avatar image Slin · Mar 19, 2010 at 02:21 PM 0
Share

I still dont get it, but I will just go the way you proposed, which should work without any problems.

avatar image Nanako · Jul 15, 2015 at 03:40 PM 0
Share

Wouldn't it be more correct to say that you can't scale an object ONLY in a specific direction?

Theoretically, you can make an object larger along any desired axis or direction, as long as you don't $$anonymous$$d it getting bigger in some others too.

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

1 Person is following this question.

avatar image

Related Questions

Global rotation on child object ? 2 Answers

Global transform position and scale for gameObject 0 Answers

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

Gradually scale platform? 4 Answers

How to smooth transform scaling with 2d sprite 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