- Home /
transform.localscale
Hi,
I have some questions I haven't been able to find the answers to yet. I'd like to find the height/width of a game object. I know that Transform.localscale will give me something similar, but as far as I know this is from the center of the object. I cannot find anything that will allow me to either get the height/width of an object or work it out via the scale.
Can someone please help?
Answer by robertbu · Jul 01, 2013 at 01:25 AM
You can use Mesh.bounds. This will be an axis aligned bounding box in local space. You will need to scale the box by localScale if you have a scale other than (1,1,1).
function Start() {
var bounds = GetComponent(MeshFilter).mesh.bounds;
Debug.Log(bounds);
}
Thanks for your comment, this seems to be what I'm looking for. However, I need a few more specifics. For instance, the second line (this maybe because I'm more used to c#) seems to be collecting a mesh filter but from somewhere I can't deter$$anonymous$$e. I have a gameObject setup in the constructor, the scale of which is different to 1,1,1 as you mentioned.
I'm guessing I'll need to create a new vector3 from the localscale which will be applied to the mesh. However, I'm not sure how I can get the mesh to apply to the specific game object I have created a code reference for in the Constructor; if that makes any sense.
Your help will be greatly appreciated, thanks for replying.
The GetComponent is using the game object the script is attached to. For C# you would get your size like this (untested):
Bounds bounds = GetComponent<$$anonymous$$eshFilter>().mesh.bounds;
Vector3 size = bounds.extents * 2.0f;
size = Vector3.Scale(size, transform.localScale);
Note that the extents are 1/2 the actual size.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
a better movement code C# 3 Answers