Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 tfalmeida91 · Oct 15, 2021 at 03:44 PM · raycasthitboxcolliderclosestedges

How to get Closest Edge/Vertex Position of a 3D BoxCollider?

Hi there! I've been trying to find a way to get the closest Edge/Vertex position of a 3D BoxCollider from a given Vector3 Position.

In this case I hit a 3D BoxCollider with a Raycast and get the Point where it hit. Now I want to get the closest Edge or Vertex of that BoxCollider to the Point of the Raycast;

Like this:

alt text

How can I do this?

Thank you in advance!

example.png (5.0 kB)
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

Answer by Bunny83 · Oct 15, 2021 at 07:30 PM

The general approach is always the same. You would first transform your position into the local space of the collider by using transform.InverseTransformPoint(). Once the point is in local space it's just a matter of determining / projecting your point onto the right element. You said edge and vertex. Those are conceptionally quite different things. So it's not quite clear what you're looking for exactly. Keep in mind that a BoxCollider has a center "offset" and a "size" besides the transform manipulations.


A default cube has a size of 1,1,1 and an offset of 0,0,0. Note that the size is the total size, not the extent. If you just scaled a default cube with the transform, this will already be handled by the inverse transform. However if your collider has a changed size / center you have to keep that in mind as well. In essence you just have to add the center to the local position of your point of interest and also divide each component by the component of the size to get to the same basis. The 8 corners are just +- 0.5


If you want to find the closest vertex / corner of the box, all you have to do is analyse the sign of the 3 components. So if x is positive, you know the closest vertex is one of the 4 corners on the right side, otherwise one of the 4 corners of the left side. Likewise if y is positive it's one of the two upper vertices, otherwise the two lower vertices. Finally the z sign tells you if it's the front or back vertex.


Determining the edges is a bit more complicated sinde a cube has 12 edges. Here it's necessary to determine which component is greater. While a specific corner has a single vertex position, an edge consists of two vertices. So the "selection" of an edge is also more complicated. Also edges are actually orientated. So it depends on your actual needs what you should do.

Comment
Add comment · Show 1 · 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 tfalmeida91 · Oct 16, 2021 at 05:57 PM 0
Share

You actually gave a good Ideia on how to approach this.

For starts, my cubes will only move and rotate side ways, so the edges will always face the same direction and further more, my rays always hit the cubes at 0f on the Y.

So I remembered, when the ray hits I could (roughly) do the following steps:

  1. By getting the size of the cube I know where each Vertex is located at, and thus I can get the closes Vertex to the Hit point;

  2. Now I find what is the Closest Vertex to my Hit Point;

  3. Given that this game is played only on XY axis (so in essence a 2D game in a 3D environment), I know where the edge will be, be cause I can get the position of the Vertex and set the Y axis to 0.

That position is where the Edge will be located.

As soon as I can figure this out, I'll post the code here!

Thank you 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

129 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 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 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 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 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

Boxcast returns one incorrect hit point of (0,0,0). 1 Answer

Collider inside another collider : I have these spheres inside the brain, and I want to be able to move the brain via mouse while being able the click the spheres 2 Answers

Why are RaycastHit surface normals of corners not interpolated or otherwise correct? 1 Answer

Raycast is not returning exact number / Mesh is not exact dimensions 2 Answers

Physics raycast hit offset from where it should be 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