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
0
Question by Caiuse · Apr 04, 2012 at 10:56 AM · collisioncontacts

Collision contacts problem

I've written a little bit code to detect when my character hits the edge of a platform:

    if(!hit.collider.gameObject.CompareTag("Platform")) return;
         if(hit.contacts[0].normal == -Vector3.forward){
             print("hit side");
         } else {
             print(hit.contacts[0].normal +" : "+ -Vector3.forward);
         }

for debugging purposes I wanted to print the normal of the ContactPoint if it doesn't equal the back of the platform. but instead of the IF statement detecting that this is the case instead it prints (0.0, 0.0, -1.0) : (0.0, 0.0, -1.0) now im my book this shows that the IF statement is getting it completely wrong... what possably could cause such a thing?

Comment
Add comment · Show 1
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 clfischer · Apr 04, 2012 at 02:04 PM 0
Share

I was going to suggest that for some reason the float values weren't quite identical but the documentation says that the == operator for Vector3 returns true "for vectors that are really close to being equal". Weird indeed.

1 Reply

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

Answer by GuyTidhar · Apr 04, 2012 at 02:01 PM

You probably need to use "Mathf.Approximately". The floating point might be very similar but does not equals due to precision on CPU/GPU levels so you need an approximate.

e.g.

 if ( Mathf.Approximately(hit.contacts[0].normal.x, -Vector3.forward.x)
      && Mathf.Approximately(hit.contacts[0].normal.y, -Vector3.forward.y)
      && Mathf.Approximately(hit.contacts[0].normal.z, -Vector3.forward.z) )
          print("hit side");
 } else 
 {
      print(hit.contacts[0].normal +" : "+ -Vector3.forward);
 }
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 Kryptos · Apr 04, 2012 at 02:23 PM 2
Share

As @clfischer pointed out, this is what is done by operator== regarding Vector3.

The problem here is that printing out the value of a Vector3 only gives 2 digits. $$anonymous$$athf.Approximately checks the 6th (or 7th) digit (after formatted to scientific: n.dddddd*10^y).

To see more digits, use the formatted string:

 print(hit.contacts[0].normal.ToString("F5") +" : "+ -Vector3.forward.ToString("F5"));

To compare the two vectors, the best way is to check the dot product. If the dot product is close to 1, then the two unit vectors are almost identical:

 float threshold = 0.99f;
 if( Vector3.Dot(hit.contacts[0].normal, -Vector3.forward) >= threshold  )
 {
     print("hit side");
 }
avatar image Caiuse · Apr 04, 2012 at 02:29 PM 0
Share

@$$anonymous$$ryptos Before even reading these responses I started using Vector3.Dot it works fairly well and even allows me to include the other sides of the object. Thanks.

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

detection cube face while rotating 0 Answers

Collision.contacts issue 1 Answer

Check collision on the sides 0 Answers

Unexpected position of contact point from sphere-sphere or sphere-capsule colliders 0 Answers

How do I use contacts in OnCollisionEnter? 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