Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheDDestroyer12 · Jan 10, 2015 at 08:05 PM · gameobjectvector3scalesizepositioning

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheDDestroyer12 · Jan 11, 2015 at 08:17 AM 0
Share

Can I do this with simple shapes too? I have an empty GameObject containing multiple cubes. Can I get the Bounds/center of it?

avatar image Owen-Reynolds · Jan 11, 2015 at 04:49 PM 0
Share

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.

avatar image _dns_ · Jan 11, 2015 at 04:49 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges