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
2
Question by thaiscorpion · Jul 09, 2012 at 01:51 AM · colliderobjectrigid bodybottom

How to specify a a rigid body colliding with the bottom area of an object

Im trying to detect from where the player is colliding with an object, if its from top or bottom. Im using this:

 function OnCollisionEnter ( other : Collision ) {
  
  if ( other.gameObject.CompareTag ( "Floor" ) ) {
  
                 //if hit from top 
             if () {
            Debug.Log ("You Hit the floor");
             }
 
                //if hit from bottom
             if () {
            Debug.Log ("You Hit the roof");
             }    
  }
 }

But I'm not sure how to detect this, any help 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
5
Best Answer

Answer by aldonaletto · Jul 09, 2012 at 02:20 AM

If the Y axis is your vertical direction (most frequent case), you can check normal.y: positive values mean collision with the bottom side, and negative values mean top collisions:

function OnCollisionEnter ( other : Collision ) {
  if ( other.gameObject.CompareTag ( "Floor" ) ) {
    var normal = other.contacts[0].normal;
    if (normal.y > 0) { //if the bottom side hit something 
      Debug.Log ("You Hit the floor");
    }
    if (normal.y < 0) { //if the top side hit something
      Debug.Log ("You Hit the roof");
    }    
  }
}
normal.y is numerically equal to the sine of the normal angle relative to the horizontal plane, thus you can refine your code by accepting floor or roof collisions only when the normal.y is higher than the sine of the angle ( < -45 or > 45, for instance, would become normal.y < -0.707 or normal.y > 0.707).

But be aware that OnCollisionEnter is only reported when a rigidbody hits a collider; if your character is a CharacterController, you must use OnControllerColliderHit instead:

function OnControllerColliderHit ( hit : ControllerColliderHit ) {
  if ( hit.gameObject.CompareTag ( "Floor" ) ) {
    var normal = hit.normal;
    if (normal.y > 0) { // if the bottom side hit something 
      Debug.Log ("You Hit the floor");
    }
    if (normal.y < 0) { // if the top side hit something
      Debug.Log ("You Hit the roof");
    }    
  }
}
NOTE: This event is sent all the time due to the contact of the character with the ground, thus you will get lots of "You hit the floor" messages.
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 thaiscorpion · Jul 09, 2012 at 02:46 AM 0
Share

Great thanks aldo that really helped me out wokrs perfeclty!

avatar image ItsTehStory · Feb 20, 2017 at 04:36 AM 0
Share

You just saved my day, I spent the whole day trying everything, from Raycasts to little BoxColliders under the player. I even added an empty object with a script on it that would detect the ground. Nothing, and I mean, NOTHING worked. I saw your post and I was like, no way that works. But it does xD... thank you and here's a picture of me during the afternoon alt text

rage-table.png (249.3 kB)
avatar image
0

Answer by thaiscorpion · Jul 09, 2012 at 06:21 AM

I found the solution using the contact.normal vector and checking if I hit from under or above

normal = other.contacts[0].normal;

//if hit floor if ( normal == Vector3 ( 0, 1, 0 ) ) {

  Debug.Log ("You Hit the floor");
 

}

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 thaiscorpion · Jul 09, 2012 at 03:04 PM 0
Share

Forget this one aldos is explained much better and has more functionality I wrote this but didnt come up until aproved by moderator :D

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to check if one object is colliding with another 1 Answer

Enable objects to enter the trigger 2 Answers

problem with collider 1 Answer

Script to move an objects position and rotation upon collision with player? 1 Answer

Find all objects with the name: "object name" and disable their colliders 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