- Home /
How do you change size of an object by its units rather than scale?
I need a game object to be exactly 10 units long in the z axis. I can try resizing the object while looking at the grids in scene mode to get a very close estimate, however when I do it that way, I can't confirm that it's exactly 10 units. It may be 10.0002 or something like that. Is there an option in Unity to do this? Or is there a way to do it with c#? Thanks for your help.
Are you trying to change the size of the mesh without changing the scale? This would require you to programmatically construct an enlargened model of the mesh.
Answer by Eno-Khaon · Jun 23, 2020 at 06:19 PM
If you know the initial size of the object (for example, Mesh.bounds if you're basing it on the extents of the object), then you can programmatically scale it to a precise target size.
public Vector3 targetSize = Vector3.one * 10.0f; // Example 10x scale
// ...
Mesh m = gameObject.GetComponent<MeshFilter>().sharedMesh;
Bounds meshBounds = m.bounds;
Vector3 meshSize = meshBounds.size;
float xScale = targetSize.x / meshSize.x;
float yScale = targetSize.y / meshSize.y;
float zScale = targetSize.z / meshSize.z;
transform.localScale = new Vector3(xScale, yScale, zScale);
well, this suggestion changes the scale, which is something that was requested to be avoided.
this answer is correct, you can only change scale object, not size, so you fake that with math, you calculate how much scale you need to achieve target object size
Your answer
Follow this Question
Related Questions
actual size of an object 2 Answers
Positioning gameobjects by a corner, not by center 0 Answers
How to align the bottom of a GameObject with the ground? 1 Answer
Reset scale of a GameObject 2 Answers
Match a GameObject's size to another's? 2 Answers