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
1
Question by veclock · Aug 19, 2012 at 07:49 PM · anglecubepointintersect

Angle to position on a cube

Hi!

I want to calculate a position on the surface of a cube, only using an angle from the center of the cube, in other words, the point a ray intersects with the cube if the ray leaves from the center of the cube.

Is there any easy way of calculating this with a quaterion, matrix or euler angle?

 void f(GameObject cube, Quaternion a) {
     Vector3 intersectPoint;
     /* calc */
     return intersectPoint;
 }

Thanks in advance!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by veclock · Aug 19, 2012 at 11:16 PM

Think I found a solution:

Vector3 p = Random.onUnitSphere*cube.transform.localScale.magnitude; if (p.x > cube.transform.localScale.x*0.5f) { p.x = cube.transform.localScale.x*0.5f; } else if (p.x < -cube.transform.localScale.x*0.5f) { p.x = -cube.transform.localScale.x*0.5f; } if (p.y > cube.transform.localScale.y*0.5f) { p.y = cube.transform.localScale.y*0.5f; } else if (p.y < -cube.transform.localScale.y*0.5f) { p.y = -cube.transform.localScale.y*0.5f; }

if (p.z > cube.transform.localScale.z*0.5f) { p.z = cube.transform.localScale.z*0.5f; } else if (p.z < -cube.transform.localScale.z*0.5f) { p.z = -cube.transform.localScale.z*0.5f; } If there's a better way, please share!

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
1

Answer by aldonaletto · Aug 20, 2012 at 01:17 AM

That's a good solution, but will fail when the cube is rotated and/or not centered at (0,0,0). Additionally, each time you read cube.transform.localScale, Unity makes calls to the internal functions getTransform and getLocalScale, what wastes time.
Your algorithm may be changed to solve all these problems:

  // get a random point p in the surface of the sphere
  // that encapsulates a cube with dimensions 1x1x1:
  Vector3 p = Random.onUnitSphere;
  // flatten the point on the 1x1x1 cube surface:
  p.x = Mathf.Clamp(p.x, -0.5f, 0.5f);
  p.y = Mathf.Clamp(p.y, -0.5f, 0.5f);
  p.z = Mathf.Clamp(p.z, -0.5f, 0.5f);
  // convert the point to the actual cube surface, according
  // to its position, rotation and scale:
  p = cube.transform.TransformPoint(p);
The first four instructions actually create a kind of Random.onUnitCube function, and the last one transform it to the actual cube surface.
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

9 People are following this question.

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

Related Questions

how to find direction between two points 2 Answers

max size of a cube at irregular angles 2 Answers

Find out x at y 0 Answers

Finding the angle between 2 clicked points 2 Answers

Make an object rotate from one end only 1 Answer


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