How to calculate width of objects?

hi folks!

is there a way to calculate the with of an object in worldspace? in AS3 you can calculate it like that:

width_of_object1 = object1.width;

so you can position another object independent from the absolute position just relative to this object by

position_of_second_object = object1.x + object1.width;

I need this in unity, too. is this possible? thanx!!

Use Mesh.bounds.size. This gives you the bounds in local space. Renderer.bounds gives you an axis-aligned bounding box so it's not as useful in most cases.

If the object has a renderer, you can use

renderer.bounds

to find the visual size. May have to multiply by localScale or push the bounds through a transform.TransformDirection function.

We use something called: transform.lossyScale.x, transform.lossyScale.y, transform.lossyScale.z

but note that some objects like cubes are 10 times the side so you ll have to multiply it by 10.

I've just done something similar, where I needed the width while the local scale was != 1.

I created two empty gameObject (let's call them "a" and "b" ) and made them children of my object. The first empty GO was placed at the center of the obect, the second one at one exetremity.

Then, w = Vector3.Distance( a.transform.position, b.transform.position );

That's the half of the width regardless of the scale, but it looks like that's what you need. I hope that help (and that's clear :x).