Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by 4illeen · Jul 18, 2011 at 04:30 PM · cubefindcorner

Finding cube corners

I have an object in front of my character which is a cube - it moves with the player and rotates on y axis when 'q' and 'e' buttons are pushed. I need to find x and z values of this cube's corners (doesn't matter if it's the bottom set or one on the top.)

I tried making Vector3 type variables and setting their values to (cube.transform.position.x - cube.transform.localscale.x / 2) same for z (pluses and minuses change depending on the corner), but that didn't work when the cube was being rotated - I used gizmos to check them. Is there any easy way to solve this?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by sneftel · Jul 18, 2011 at 04:40 PM

cube.transform.localScale / 2 will indeed get you the local position of one of the cube's corners; Vector3.Scale(cube.transform.localScale / 2, new Vector3(1,1,-1)) will get you one of the others (different combinations of 1 and -1 there will get you all eight). Then, to find them in world space, use cube.transform.TransformPoint().

Comment
Add comment · Show 2 · 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 4illeen · Jul 18, 2011 at 05:43 PM 0
Share

could you pleas answer my second question?

avatar image sneftel · Jul 18, 2011 at 05:58 PM 0
Share

Sorry, that code snippet isn't quite correct. Pass new Vector3(1,1,-1) or whichever directly to TransformPoint(), rather than running it through Scale.

avatar image
0

Answer by 4illeen · Jul 18, 2011 at 05:35 PM

It worked thanks - but I've got another problem. When I set cube's scale in Start() cube.transform.localScale = new Vector3(2, 1, 1); something weird happens. Here's the pic http://img845.imageshack.us/img845/8425/cubea.png

, seems like whenever the scale value isn't equal to 1 it adds to the position of the point somehow.

Comment
Add comment · 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
0

Answer by jordanmisiura · Oct 31, 2011 at 06:06 AM

instead of local scale, why not get the mesh filter, then get it's bounds?

MeshFilter mshFilt = ((MeshFilter)this.GetComponent()); Mesh cubeMesh = mshFilt.mesh;

someTransform.transform.position = this.transform.TransformPoint(cubeMesh.bounds.min);

using bounds.min and bounds.max should get you two flavors of the cube's corners, and taking various values from the min and max's x,y,z coords should get you the other six.

Comment
Add comment · 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
0

Answer by jordanmisiura · Oct 31, 2011 at 06:06 AM

MeshFilter mshFilt = ((MeshFilter)this.GetComponent()); Mesh gridMesh = mshFilt.mesh;

yellowCube1.transform.position = this.transform.TransformPoint(gridMesh.bounds.min);

that should get you one of the lower corners. Using bounds.max should get you one of the upper corners on the other side probably.

Using various flavors of the max and min's x,y,z coords should get you all 8 corners.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

C# sticking to Cube corners position (raycasting AI script) 1 Answer

How would I go about making a cube "tumble" off of corners and such? 2 Answers

Finding the dimensions of a cube, exploiting them. 2 Answers

Recover project from a different computer? 1 Answer

How do i find a bone on my character within a script? 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