- Home /
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)
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.
you can try this (i dont know why my reply was deleted..) http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnCollisionStay2D.html
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
}
}
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;
}
}
Exactly what I was lookin for! But, how would I assign a game object in script?
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>();
}
Thank you so much! I'm making a project, and this will be very helpful for me! =)
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.
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
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.
Your answer

Follow this Question
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