3
\$\begingroup\$

I'm trying to get the distance from the BoxCollider center of a gameobject to the bottom of that same BoxCollider. Note that the center of the collider is slighty modified.

enter image description here

So far, I have:

float halfHeightCollider = (bounds.size.y / 2);

I tried the following to take care of the slight modification:

float halfHeightCollider = bounds.center.y + (bounds.size.y / 2);

But seems like bounds.center give me the position of the center of the BoxCollider in the world, not relative to the gameobject, like in the inspector:

enter image description here

That value is the one I want. How can I get it?

\$\endgroup\$
4
\$\begingroup\$

You can get the center in y-axis from gameObject.GetComponent<BoxCollider>().center.y.

You will get the total size in y-axis of the collider from gameObject.GetComponent<BoxCollider>().size.y.

Then take the y-size/2 and subtract center value in y-axis from it.

\$\endgroup\$
  • \$\begingroup\$ Okay, now I get what I was doing wrong. I store the BoxCollider bounds reference in a variable and that value wasn't getting updated. Thank you! \$\endgroup\$ – Gardener Jun 29 '16 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.