- Home /
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.
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
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);
}
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 ;)
I spent a whole day trying to achieve this result and failed until I found this answer. It works like magic! Thank you.
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.
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
Follow this Question
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