x


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!

more ▼

asked Jul 09 '12 at 01:51 AM

thaiscorpion gravatar image

thaiscorpion
164 4 10 13

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Jul 09 '12 at 02:20 AM

aldonaletto gravatar image

aldonaletto
41.4k 16 42 197

Great thanks aldo that really helped me out wokrs perfeclty!

Jul 09 '12 at 02:46 AM thaiscorpion
(comments are locked)
10|3000 characters needed characters left

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");

}

more ▼

answered Jul 09 '12 at 06:21 AM

thaiscorpion gravatar image

thaiscorpion
164 4 10 13

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

Jul 09 '12 at 03:04 PM thaiscorpion
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1696
x1094
x32
x9

asked: Jul 09 '12 at 01:51 AM

Seen: 685 times

Last Updated: Jul 09 '12 at 03:04 PM