- Home /
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...
I implement the below scale matrix in an arbitrary direction in shader:
But it seems wired and means no sense.
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.
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?
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.
I still dont get it, but I will just go the way you proposed, which should work without any problems.
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.