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 DubSket · Jul 01, 2013 at 01:17 AM · c#transform

transform.localscale

Hi,

I have some questions I haven't been able to find the answers to yet. I'd like to find the height/width of a game object. I know that Transform.localscale will give me something similar, but as far as I know this is from the center of the object. I cannot find anything that will allow me to either get the height/width of an object or work it out via the scale.

Can someone please help?

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
0
Best Answer

Answer by robertbu · Jul 01, 2013 at 01:25 AM

You can use Mesh.bounds. This will be an axis aligned bounding box in local space. You will need to scale the box by localScale if you have a scale other than (1,1,1).

 function Start() {
      var bounds = GetComponent(MeshFilter).mesh.bounds;
      Debug.Log(bounds);
  }


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 DubSket · Jul 01, 2013 at 01:39 AM 0
Share

Thanks for your comment, this seems to be what I'm looking for. However, I need a few more specifics. For instance, the second line (this maybe because I'm more used to c#) seems to be collecting a mesh filter but from somewhere I can't deter$$anonymous$$e. I have a gameObject setup in the constructor, the scale of which is different to 1,1,1 as you mentioned.

I'm guessing I'll need to create a new vector3 from the localscale which will be applied to the mesh. However, I'm not sure how I can get the mesh to apply to the specific game object I have created a code reference for in the Constructor; if that makes any sense.

Your help will be greatly appreciated, thanks for replying.

avatar image robertbu · Jul 01, 2013 at 01:50 AM 0
Share

The GetComponent is using the game object the script is attached to. For C# you would get your size like this (untested):

 Bounds bounds = GetComponent<$$anonymous$$eshFilter>().mesh.bounds;
 Vector3 size = bounds.extents * 2.0f;
 size = Vector3.Scale(size, transform.localScale);

Note that the extents are 1/2 the actual size.

avatar image DubSket · Jul 01, 2013 at 02:12 AM 0
Share

Thanks for the help!

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

15 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

a better movement code C# 3 Answers

Not finding closest Object with tag player. 2 Answers

Why is transform.LookAt backwards for my model? 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