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:
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):
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:
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...
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:
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.