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
3
Question by amanunity · May 07, 2013 at 02:05 PM · rotationcolliderboxcollider

Collider closestPointOnBounds returning point that is not touching the collider

ClosestPointOnBounds is returning a point that is away from the collider and not touching the collider. See the laser below that is not touching the white box. This happens whenever I have a box at an angle. as shown below.

alt text

Here is the code in the laser object:

Vector3 m_targetPos = m_target.collider.ClosestPointOnBounds(transform.position);

LineRenderer m_line.SetPosition (0, transform.position);

m_line.SetPosition (1, m_targetPos);

collider not hitting.png (36.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

2 Replies

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

Answer by CHPedersen · May 07, 2013 at 02:16 PM

This happens because the collider's bounding volume is axis-aligned. Unfortunately, the collider doesn't come with a built-in way to solve this.

There is another question here that appears to tackle a similar issue. In this question, Duck shares an algorithm that calculates the closest vertex on the mesh. This might still not suit your purpose, however, since the box is a very simple mesh, all of whose vertices are at the corners. So the laser would only target the corners. But maybe it will work for you later in your project, if your models are more complex? (I'm assuming you're using the box as a temporary model during development)

In any case, I don't know exactly what kind of behavior you want this laser to exhibit, so bear with me... But I think you're going about this the wrong way to begin with. Usually with lasers, people do raycasts from the laser's position in some firing direction, and then evaluate the ray cast against the colliders. The returned RayCastHit object has the exact point. Maybe this is actually more like what you need?

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 amanunity · May 07, 2013 at 02:37 PM 0
Share

Thanks for the explanation. Collider.RayCast works perfectly.

P.S: I used collider of the cube because I am targeting only the cube. Thanks for answering my question so quickly.

avatar image CHPedersen · May 07, 2013 at 02:41 PM 0
Share

Super. If you feel it helped, it would be great if you could mark the answer accepted, please. :)

avatar image WidmerNoel · Jun 19, 2014 at 09:17 AM 0
Share

for someone that may has this problem in 2d: ins$$anonymous$$d of getting the bounds of the renderer use the bounds of the renderers sprite. Those are NOT axis-aligned! Still, great to see that some people were dealing with the same problems that I had lately :D

avatar image
3

Answer by demonpants · May 07, 2014 at 06:11 PM

What I'm doing is using Unity's closestPointOnBounds to get a direction from my target to the collider, then doing a raycast from there to get the specific point. This works well enough for my purposes, but in some cases will yield no point because the bounds may be outside the edges.

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 WidmerNoel · Jun 19, 2014 at 09:23 AM 0
Share

this is a pretty good idea! Because I need the exact magnitude between the two objects. Now I just need to find a way of fixing the issue you mentioned. thx!

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

14 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

Related Questions

What is the advantage of using Box Collider with a center of (0.5, 0.5, 0.5) 1 Answer

Stop sword from distorting character animation 3 Answers

How to rotate a mesh collider? 0 Answers

A script that auto generated the box collider around a 3d model 3 Answers

Tilemap Collider wrong rotation 0 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