Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
4
Question by JoshuaTheJoker · Jan 27, 2016 at 03:01 PM · physics2dphysics 2d

How to see if two objects are touching each other

So, I am trying to check if two objects are touching each other, and this is the part of the script that I cant get to work

     public static bool IsTouchingS (BoxCollider2D.StraightS, BoxCollider2D.PlayerCar);
 
     void GetLocator ()
     {
         if (IsTouchingS == true)

Comment
Add comment · Show 3
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 FortisVenaliter · Jan 27, 2016 at 07:12 PM 0
Share

I get the feeling you're missing some code here... really hard to diagnose without more info.

Also, you should mention how it's not working. How you've tested it, etc.

avatar image GKiernozek · Jan 27, 2016 at 07:42 PM 0
Share

you can try this (i dont know why my reply was deleted..) http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnCollisionStay2D.html

avatar image JoshuaTheJoker · Jan 27, 2016 at 09:20 PM 0
Share

Ill try to elaborate, I want the game to check if PlayerCar is touching S, if it is, then do this

     public BoxCollider2D PlayerCar = GameObject.FindGameObjectWithTag("PlayerCar");
     public Collider2D S = GameObject.FindGameObjectWithTag("S");
 
     void GetLocator ()
     {
         if(PlayerCar.IsTouching(S))
         { 
             //DO SO$$anonymous$$ETHING
         }
     }

4 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by brunocoimbra · Jan 27, 2016 at 07:47 PM

There is already a function for this inside the Collider2D class:

 public Collider2D objectCollider;
 public Collider2D anotherCollider;
 
 private void SomeMethod()
 {
     if (objectCollider.IsTouching(anotherCollider))
     {
         // Do something;
     }
 }
Comment
Add comment · Show 3 · 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 JoshuaTheJoker · Jan 27, 2016 at 09:11 PM 0
Share

Exactly what I was lookin for! But, how would I assign a game object in script?

avatar image brunocoimbra JoshuaTheJoker · Jan 28, 2016 at 03:37 PM 0
Share

You can either pass the gameObject in the inspector, or you can use this:

 void Start()
 {
     objectCollider = GameObject.FindGameObjectWithTag("PlayerCar").GetComponent<Collider2D>();
     anotherCollider = GameObject.FindGameObjectWithTag("S").GetComponent<Collider2D>();
 }
avatar image partidosolo · Sep 16, 2020 at 05:11 PM 0
Share

Thank you so much! I'm making a project, and this will be very helpful for me! =)

avatar image
0

Answer by LLIV · Jan 28, 2016 at 09:53 AM

Well you could do one of two things. You could find the distance between the objects and decide if they're touching based off that. Obviously this doesn't actually tell you if they're touching but it's one method. You could also use your box colliders and listen for collisions.

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 GKiernozek · Jan 28, 2016 at 01:12 PM

Try OnCollisionStay2D for checking for two object touching each other, more info and example code here: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionStay2D.html

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
0

Answer by LGC7767 · Oct 07, 2021 at 03:26 AM

Hello, This post was semi solved a while ago, but it didn't work for me so I figured I'd post the solution I came up with and explain why this solution didn't work for me.

This solution didn't work for me because my object is 3d and can be touching two objects at the same time. Most other posts try to get by this by verifying the touching object with a tag. However I had the possibility of an object touching two items with the same tag at the same time. For example, I needed my player to be able to jump. However I only want them to be able to jump if they were touching the environment. The player could both be touching the ground and some stairs at the same time. If the player was touching the side of stairs while standing on the ground, then when the player stopped touching the stairs, they would be unable to jump. However they would still be touching the ground, so logically they should be able to jump. Heres my solution to this problem:

First the basics:

     private bool canJump = false;
     FixedUpdate(){
     if (canJump)
             {
                 if (Input.GetKey(KeyCode.Space))
                 {
                     Rigidbody.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
                    
                 }
             }
     }
 

This makes it so the player will jump when the bool is true, and by default the bool is false. I have the player start above the map and fall into it so that they start with the onCollision I'm about to write:

Start by adding to your variable list:

       private GameObject[] touchingObjects;
 

Then add to your Start():

 private void Start()
     {
         touchingObjects = new GameObject[10];
      }

Finally the OnCollision Enter and Exit:

  private void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.tag == "Environment")
         {
             int tempint = touchingObjects.Length;
             GameObject[] tempArray = new GameObject[tempint+1];
             for(int i = 0; i< tempint; i++)
             {
                 tempArray[i] = touchingObjects[i];
             }
             tempArray[tempint] = collision.gameObject;
             touchingObjects = tempArray;
             canJump = true;
         }
     }
     private void OnCollisionExit(Collision collision)
     {
         if (collision.gameObject.tag == "Environment")
         {
             canJump = false;
             System.Collections.Generic.List<GameObject> list = new System.Collections.Generic.List<GameObject>(touchingObjects);
             list.Remove(collision.gameObject);
             touchingObjects = list.ToArray();
             for(int i = 0; i < touchingObjects.Length; i++)
             {
                 if (touchingObjects[i])
                 {
                     if (touchingObjects[i].tag == "Environment")
                     {
                         canJump = true;
                     }
                 }
             }
         }
     }

This way, all objects that come into contact with the player object are added to an array. When the object is no longer touching, they are removed from the array. When they are removed, the player will no longer be able to jump unless one of the objects they are still touching also has the "Environment" tag.

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

12 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

Related Questions

2D Physics and Jump-Through Platforms 2 Answers

How to make Two colliders, don't collide, but still be able of interact with each others 2 Answers

deltaTime doesn't work with custom gravity (ECS) 0 Answers

Snake Movement Like in Snake vs Box 2 Answers

Rigidbody2D is kind of flying instead of falling when the child box is colliding 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