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
3
Question by regnared · Jan 09, 2013 at 07:53 PM · c#collisioncolliderbox collider

How to detect which side of a box collider was hit? (Top, Bottom, Right, Left) 2D game/ C#

I've been thinking about this for the past two days. Been searching on the net but can't find anything.

I came up with a code using empty game objects to show the sides of the box, but I'm sure there's a more optimal and good way to do this. It's pretty basic stuff but can't find anything on the subject. I've tried collision contact points, but there was a bug where if the top part had a collision, the LEFT or RIGHT side was also a collision.

Is there an efficient/clean way to discover which sides of a box collider had a collision, or am I better using raycasts? In my game, the player is using raycasts, having no problem detecting the side of the player which has a collision. But with a box collider it's a bit more complex it seems.

Thank you for anyone considering helping me. :)

alt text

box_sides.jpg (102.1 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by toddisarockstar · Feb 22, 2018 at 09:33 PM

why not simply compare the positions of the two objects?

     void OnCollisionEnter(Collision c)
 
     {  // get the direction of the collision
         Vector3 direction = transform.position - c.gameObject.transform.position;
         // see if the obect is futher left/right or up down
         if (Mathf.Abs (direction.x) > Mathf.Abs (direction.y)) {
 
             if(direction.x>0){print("collision is to the right");}
             else{print("collision is to the left");}
         
         }else{
 
             if(direction.y>0){print("collision is up");}
             else{print("collision is down");}
 
         }
 
 
     
     }
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 jstam · Aug 20, 2019 at 11:43 AM 0
Share

Because it may collide with a Tilemap collider, where the position of the entire tilemap is not relevant for the collision direction

avatar image GregoryFenn · Aug 27, 2019 at 08:51 AM 0
Share

This is generally a poor method. Consider a cube object for the floor: if you fall on it, the collision is direction below you, on your feet, neither left nor right. But the center of the floor could be anywhere. Or you might run leftwards into an L-shaped object, where the center pivot of the object is actually to your right.

Basically the pivot's transform.position can't generally be assumed to be a good indication of where a collision is. Unless the object is simple and convex like a cube or sphere.

avatar image
2

Answer by regnared · Jan 17, 2013 at 02:40 AM

If anyone finds this question, I was able to use raycasting for my problem. I posted a script and video here if you are curious.

Youtube Link - [link ]

Script Link - [link ]

Comment
Add comment · Show 5 · 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 Bunny83 · Jan 18, 2013 at 12:06 AM 1
Share

You are kidding, right? He posted a link to a youtube video in the answer above. In the video description he posted the link to his script

avatar image AntFitch · Mar 10, 2014 at 04:33 AM 1
Share

Thank you, this was very helpful, regnared!

avatar image regnared · Mar 10, 2014 at 05:38 AM 0
Share

Glad it can be useful! :)

avatar image smash228 · Feb 22, 2018 at 06:46 PM 0
Share

This is a bad way to do it. You should ins$$anonymous$$d place one box collider on each side to do the detection. It does involve a lot of box collider, but it will be incredibly faster than using raycasts for no valid reason.

avatar image meat5000 ♦ smash228 · Feb 22, 2018 at 06:50 PM 0
Share

Yep this is old but hey. $$anonymous$$y brain tells me that finding local contact point would be simple; its in the hit info. From origin of object its just a simple case of finding the direction to that point and deciding which face its on based on the axes.

avatar image
1

Answer by jtagrawal2000 · Oct 09, 2020 at 07:15 AM

Use this to look for direction of collision If this is included in script of a object which collides with c then this will print which side of c did a hit , you can also look to see if it hit the corners by checking the direction’s values

   void OnCollisionEnter(Collision c)
   {
      Vector2 direction = c.GetContact(0).normal;
      If( direction.x == 1 ) print(“right”);
      If( direction.x == -1 ) print(“left”);
      If( direction.y == 1 ) print(“up”);
      If( direction.y == -1 ) print(“down”);
   }
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

18 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

Related Questions

Getting a sword in place while attacking an enemy 3 Answers

Surface with hole and Raycast - Which collider 1 Answer

Objects with colliders going through walls and each other. 3 Answers

Check if trigger is occupied 1 Answer

Allow picked up object to collide with game level GameObjects 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