Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 vardevelopment · Jul 13, 2017 at 09:22 PM · raycastraycasthitboxcolliderhit.point

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

I am trying to select faces of a cube with raycasting. I converted the RaycastHit.point to a local vector relative the transform of the collider hit. The box collider I created is exactly 0.2 units. Therefore when I got my RaycastHit.point and I converted it to local coordinates, I tried to use the Vector3 to select the face. So for example the vector (0,0.1,0) would be the top of the cube. The problem is that when I log the vector axes, the value that should be 0.1 (The face) is actually logged as 0.0999999. I can't even use this number to choose the faces because it is infinitely repeating and I have no idea how to solve this problem. Any advice would be greatly appreciated.

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

Answer by Kishotta · Jul 13, 2017 at 09:44 PM

So this is actually due to how floating point numbers are handled in computers (not Unity specifically). Floating point numbers are expressed as powers of 2.

0.125 is a power of 2 (2^-3) but 0.1 is not and can therefor not be perfectly represented with a float (or double for that matter). This is why you should never check equality between floating point numbers. They just can't be represented in the number of decimal places you have.

That being said, you can use Mathf.Approximately to check if floating point numbers are "close enough" to one another.

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 vardevelopment · Jul 13, 2017 at 10:20 PM 0
Share

I was going to try that but I wasn't sure if that was the right way to do it. Anyway, thank you very much for the information on how floating point numbers are handled. I had no idea they worked like that and I will surely remember that from now on.

avatar image Kishotta vardevelopment · Jul 13, 2017 at 10:29 PM 0
Share

It may be way more in-depth than you care to get, but this converter shows exactly what happens at the per-bit level.

The $$anonymous$$antissa is a way of "scaling" the power of 2. You'll notice when you enter values like 2, 4, 16, etc that the $$anonymous$$antissa is 1. But when putting in something like 5, the $$anonymous$$antissa scales up a smaller power of 2 (2^2).

avatar image vardevelopment · Jul 14, 2017 at 06:56 PM 0
Share

Okay, so your solution worked some of the time but it didn't always return true. I ended up just specifying a range that deter$$anonymous$$es the selected face. Here is one snippet out of the code:

 if (hitPosition.z > 0.099f && hitPosition.z < 0.11f)
         {
             placePosition = Vector3.forward * 0.2f;
             selectedFace = "front";
         }

This ended up working every time. I really appreciate all of the help and information from everyone though :)

avatar image
0

Answer by Cornelis-de-Jager · Jul 13, 2017 at 09:54 PM

Well it seems the number is close enough each time so just round it:

 double num =  Math.Round (yourNum, 1); // Rounds to first decimal
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

96 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

Related Questions

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

How to add a random number to my AddForce angle 0 Answers

Raycast won't hit Collider 1 Answer

Not getting right value; ScreenPointToRay 0 Answers

help the build button in the build settings is grayed out 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