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 Catlard · Nov 12, 2011 at 11:20 AM · collisionnormalcollision.contactscontact.point

Not detecting a simple collision?

Hey, folks! I might be crazy, but...for me, the first print statement will print "Normal was: (0,1,0)", but the second print statement then prints nothing. What am I doing wrong, here? I'm not really sure what the heck is up. Why would it print that statement, but then not respond to the conditional?

 function OnCollisionEnter(collision: Collision)
     {
         for (var contact : ContactPoint in collision.contacts) 
         {    
             print("Normal was:" + contact.normal);
             if(contact.normal == Vector3(0.0, 1.0, 0.0))
             {
                 print("Detected collision!");
                 }
         }
             
     }
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
2
Best Answer

Answer by Owen-Reynolds · Nov 12, 2011 at 03:08 PM

It's a float issue. The normal is a computed value, so is probably something like (0, 0.999999, 0.00001). The inspector only shows 1 decimal. You can print contact.normal.x*100000 to see more. But, even regular prints often "round to the nearest" on the screen.

The official computery way is to check: if(Mathf.Approximately(contact.normal.x, 0) && Mathf.Approximately(contact.normal.y, 1) ...)

An example of float rounding: 1/3 is 0.3333 with 3's forever. The computer can only store about seven of them. So (1/3)*3 is 0.9999999. Additionally, the computer uses binary, so really stores the 1/2's place, the 1/4ths place, the 1/8ths place ... . One fifth is the non-repeating decimal 0.2 for us, but 1/5th in binary is 1/8 + 1/16 + 1/128 ... and repeats, so has to be rounded to the nearest.

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 aldonaletto · Nov 12, 2011 at 04:40 PM 0
Share

I missed that! You're right: comparing floats equality almost never results true, unless both floats were generated in the same manner (both are constants, for instance). For Vector3 comparison, I would use a margin error:

   if ((contact.normal-Vector3(0.0, 1.0, 0.0)).magnitude < 0.001){
       ...
avatar image Catlard · Nov 12, 2011 at 05:24 PM 0
Share

You gentlemen are geniuses.

I'm trying to make it so that my character can jump through ceilings and land on them as floors--like in most platfor$$anonymous$$g games...I guess I'll start another topic! Thanks again.

avatar image
0

Answer by aldonaletto · Nov 12, 2011 at 11:46 AM

Check if that damned Collapse button in the Console pane isn't pressed: if you click this SOB button by mistake, the Console doesn't show repeated lines, driving us crazy.

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 Catlard · Nov 12, 2011 at 11:58 AM 0
Share

Nope! Not a collapse button issue, I don't think. It's not pressed. And second of all, the second print statement is different from the first--so it wouldn't be collapsed, right?

Does this code work for you? I feel like I'm taking crazy pills here.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Since unity is not recommending using collision.contact as it produces memory garbage how should I then get the point of collision? 1 Answer

Finding collision force not working in certain situations. 0 Answers

Collision.contacts issue 1 Answer

How to use contact.point from 2 seperate colliders in a collision? 0 Answers

How to parent gameobject at point of collision? 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