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 Emmerick · Jun 13, 2015 at 01:34 PM · collision detectionboxcollider2d

How can i detect which side of a box collider 2D the collider object collided with?

I have 2 gameobjects each with a box collider2d and rigidbody2d attached to them. If the first gameobject(player) collides with the second gameobject i would like to know on which side was the collision on the second object so i can move the second object in some direction.

Thanks in advance.

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
1

Answer by Spartiate0033 · Mar 29, 2016 at 05:36 AM

You may want to consider creating child objects of the Player and both of these child objects would have box collidors on them set to triggers. You'd have the left child object and the right child object. Both objects would have a script attached and they would simply contain a OnTriggerEnter() function. When triggered it would fire a message to the player script. I recommend having the player script a reference in the child object scripts for quick and efficient access.

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
avatar image
0

Answer by Addyarb · Jun 13, 2015 at 02:54 PM

Raycasting when an object is hit is the way I would approach that. You could also use triangle indexes, but that could be a little complicated, and inefficient if you're just using a true 6-sided cube.

Here's a little example script.

EDIT: This works for 3D, but I'm sure you could edit it to work for 2D as well. Just take out the forward and back block bits.

Good luck!

     public Transform blockAbove;
     public Transform blockBelow;
     public Transform blockRight;
     public Transform blockLeft;
     public Transform blockForward;
     public Transform blockBack;
 
 
 void OnTriggerEnter(){
 CheckSurroundingBlocks();
 }
 
 void CheckSurroundingBlocks(){
         //First let us raycast upwards to check for block above us...
         Vector3 up = transform.TransformDirection (Vector3.up);
         RaycastHit hitUp;
         //If we cast a ray up and hit something...(we ray cast twice this object's extents, or size of the object).
         if (Physics.Raycast (transform.position, up, out hitUp, GetComponent<Renderer> ().bounds.extents.x*2)) {
             //the block above us is that block and that block's tag is "BlockTag"...
             if(hitUp.collider.tag == "BlockTag")
             blockAbove = hitUp.transform;
         }
         //repeat for the other directions..
         Vector3 down = transform.TransformDirection (Vector3.down);
         RaycastHit hitDown;
         if (Physics.Raycast (transform.position, down, out hitDown, GetComponent<Renderer> ().bounds.extents.x*2)) {
             if(hitUp.collider.tag == "BlockTag")
             blockBelow = hitDown.transform;
         }
         Vector3 left = transform.TransformDirection (Vector3.left);
         RaycastHit hitLeft;
         if (Physics.Raycast (transform.position, left, out hitLeft, GetComponent<Renderer> ().bounds.extents.x*2)) {
             if(hitUp.collider.tag == "BlockTag")
             blockLeft = hitLeft.transform;
         }
         Vector3 right = transform.TransformDirection (Vector3.right);
         RaycastHit hitRight;
         if (Physics.Raycast (transform.position, right, out hitRight, GetComponent<Renderer> ().bounds.extents.x*2)) {
             if(hitUp.collider.tag == "BlockTag")
             blockRight = hitRight.transform;
         }
         Vector3 ahead = transform.TransformDirection (Vector3.forward);
         RaycastHit hitAhead;
         if (Physics.Raycast (transform.position, ahead, out hitAhead, GetComponent<Renderer> ().bounds.extents.x*2)) {
             if(hitUp.collider.tag == "BlockTag")
             blockForward = hitAhead.transform;
         }
         Vector3 back = transform.TransformDirection (Vector3.back);
         RaycastHit hitBack;
         if (Physics.Raycast (transform.position, back, out hitBack, GetComponent<Renderer> ().bounds.extents.x*2)) {
             if(hitUp.collider.tag == "BlockTag")
             blockBack = hitBack.transform;
         }
     }


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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to check if BoxCollider2D collided with another BoxCollider2D? 2 Answers

Player sprite clipping through game boundary in 2d scene 1 Answer

Coin collection script (not working) 1 Answer

Object passes through collider even when isTrigger is turned off. 2 Answers

Particles only colliding with one of the player's two colliders? 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