- Home /
How to align the bottom of a GameObject with the ground?
Hello!
I'm creating a simple strategy game and I use a Raycast
to determine the location that the selected unit(s) should be moved to. I wonder how I can align the bottom of each unit with the ground, because if I move the object to the location of the hit, it is being placed in the ground.
I have tried adding half of the object height ( transform.localScale.y / 2
) to the Y coordinate, but it doesn't really work.
All help is appreciated!
// TheDDestroyer12
Answer by _dns_ · Jan 10, 2015 at 09:08 PM
Hi, it depends where is the "pivot" of your object. The pivot is the local (0,0,0) of the object but it can be anywhere depending on the geometry + the object's scaling, rotation etc... You can visualize the pivot by selecting an object and selecting "pivot" instead of "center" in the upper left corner of Unity's window (under the menu)
Now, there is also a property of a mesh that can help you: the bounds. At import time, Unity will compute an axis aligned bounding box that will contain all vertices of the mesh. Just in case, you can ask to recompute it in the Start() of a component for example.
To acces the bound: use GetComponent to get the MeshFilter component and use meshfilter.sharedMesh (using meshfilter.mesh could duplicate the mesh and make it unique for your object, you don't want that)
Now, you have a variable "Bounds b" that contains the mesh bounding box. You can now have access to the center of the object with b.center, and to the height of the object with b.extends.y * 2. So, the center of the box on the "ground" will be at Vector3 lowerCenter = b.center + Vector3( 0, -b.extends.y, 0) in object space. I assume that you don't have rotations around X or Z (around Y would not change anything) but you may have scaling. Then, just multiply b.extends.y by transform.localScale.y before adding the result to b.center to compute lowerCenter.
To position the object on the ground at, say, Vector3 pos, set the transform.position = pos - lowerCenter;
Can I do this with simple shapes too? I have an empty GameObject
containing multiple cubes. Can I get the Bounds
/center of it?
You almost always just want the (000)/pivot to be at the bottom "feet go here" position. That's easy to do by shifting children.
The only time I've ever had a center-pivot for "place on ground" objects is when I needed them to also spin around the center (for all sorts of rotations, not just from the top.) In that case, the collider.bounds method is messy, but does work.
Yes, you can find all $$anonymous$$eshFilter components in the child objects and "merge" their meshes bounds. To do this, copy the first bounds and encapsulate the bounds of other child objects in it. You may have to scale those child bounds if your child objects are scaled (multiply the bounds extends by the child's localScale).
If the child objects are rotated, it becomes a little more complicated as you'll have to transform all vertices of the cube represented by the child bounds in world space and then transform them back in the parent's object space before "encapsulating" them in the parent's bounds.
Then, when you have this parent's bounds, you can compute the "lowerCenter" as described before.
EDIT: Owen's method is more simple when you have child objects! Didn't think about that ;-)
Your answer
Follow this Question
Related Questions
Positioning gameobjects by a corner, not by center 0 Answers
How do you change size of an object by its units rather than scale? 1 Answer
actual size of an object 2 Answers
Reset scale of a GameObject 2 Answers
Match a GameObject's size to another's? 2 Answers