How to get center of an object

Hi,

I want to use an cube(lets call “A”) to move other objects(cube or capsule). Other objects will be moved when the object “A” is forwarding closely to these objects. Thus, i want to get the distance between “A” and other objects. So, i want to know the center coordination of an object? Thanks.

2 Answers

2

If your models aren’t centered. transform.renderer.bounds.center

Brilliant general solution, would up-vote but not enough karma :P

–

I'll up-vote it for you, Matt_NB.

–

Thanks. In 2017 this code is gone, instead you can use GetComponent< Renderer>().bounds.center

–

Renderer can no longer be found in Transform. GetComponent<> has to be used, as pointed out above!

–

and to get the center (average idk) of a position of a transform with multiple mesh renderes inside of it? Unity has this function when you change it from Pivot to Center in the handle position

–

transform.position. The only way that wouldn’t be the center is if you use off-center models that were not modeled around the origin.

Thanks, man

–

It's perfectly valid not have model without the origin in center, e.g. a swing door with the origin on the rotation axis.

–

transform.position gives you the origin of the object, not the center, origin is almost never the center of an object

–