- Home /
Question by
geronika2004 · Jul 04, 2011 at 09:26 AM ·
scaleboundslength
Scale object to certain length
How make object scale to match exact length of its bounds property ?
For example, I need myobject.GetComponent().bounds.size.z to make equal of 5.0f, how can I reach it?
Thank you in advance.
Comment
Answer by Paulius-Liekis · Jul 04, 2011 at 10:15 AM
Something like this could work:
float targetSize = 5;
float currentSize = myobject.GetComponent().bounds.size.z; // I'm not sure if this compiles (I guess you need to specify the component type)
Vector3 scale = myobject.trasform.localScale;
scale.z = targetSize * scale.z / currentSize;
myobject.trasform.localScale = scale;
Here's how you would scale an object's height in worldspace to take up 80% of your camera:
float targetSize = Vector3.Distance(comicCamera.ViewportToWorldPoint(new Vector3(0f, 0.1f, 0f)), comicCamera.ViewportToWorldPoint(new Vector3(0f, 0.9f, 0f)));
float currentSize = planePrefab.renderer.bounds.size.y;
Vector3 newScale = planePrefab.transform.localScale;
newScale.y = targetSize * newScale.y / currentSize;
planePrefab.transform.localScale = newScale;
That becomes tricker. Probably the easiest thing to do is to unparent before you transform. childObject.transform.parent = null;
Your answer
![](https://koobas.hobune.stream/wayback/20220612132415im_/https://answers.unity.com/themes/thub/images/avi.jpg)