"If Object1 is overlapping/colliding with Object2" Help

Hello all!

I’m having troubles with a certain aspect of my game that involves the player clicking the mouse when one sprite is overlapping the other (I’m using the 2D workspace).

I figured, since both sprites are on the same Z axis and what not, I’d be able to simply use Colliders to detect when they’re colliding. This however was not the case as I tried using the OnCollisionStay2D function and it returned no results. This however seems to be more of a problem with the function as when I made an ‘If’ statement that printed that the two objects were touching ‘Else’ print “they’re not touching”. Neither of those printed…

Does anyone have any ideas as to how I can implement this? Basically what I want is that, if the player presses the mouse button when object1 is overlapping object2 it adds a point, if theyre not touching, the player loses.

Thanks in advance :smile:

EDIT: This seriously has to be the weirdest rule I’ve encountered. It turns out the reason why it wasn’t being detected is because it was kinematic. Sorry everyone if this was a really nooby thing to miss but yeah, it was kinematic.
I got around the gravity thing by setting the RigidBody2D component’s “Gravity Scale” thing to 0 and everything is working fine and dandy now.
Oh and if you’re in the same situation that I was, please don’t forget to make one of them a trigger if they are going to be overlapping!

Are the Colliders you’re using Collider2D-based or are they Collider-based? (do the components have “2D” at the end of their name?)

You can try this:

private bool hasCollision;
// Use this for initialization
void Start () {
	hasCollision = false;
}
void OnCollisionEnter2D(Collision2D coll) {
	if(coll.transform.tag == "tagtocheck"){
		hasCollision = true;	
	}
}
void OnCollisionExit2D(Collision2D coll) {
	if(coll.transform.tag == "tagtocheck"){
		hasCollision = false;	
	}
}
// Update is called once per frame
void Update () {
	if(Input.GetMouseButtonDown(0)){
		if(hasCollision){
			//addPoints
		}
		else {
			//loose
		}
	}
}

Yes they both have the 2D collider components.

Thanks for that idea and the bool variable will probably carry on into the game so thank you a lot!
However, when both the objects collide the bool doesn’t turn into true, instead, it’s always false… any ideas as to why this happens?

Also, if anyone knows a way I can easily just put object1 in front of object2 and just see when object1 is in front of object2 that’d be great. I tried using raycasting but for one thing I can barely understand what the hell is going on and for another, object1 is much smaller than object2.

Thanks for the replies :slight_smile:

do they both have rigidbodies?
both aren’t kinematic?
both can collide in the 2d layer matrix?

sounds like the function isn’t even being called

A quick little update. I’ve tried using triggers and making object1 the trigger so it goes through object2 but I’ve still had no luck. In the script, I set it so that, when object2 enters the trigger of object1, it prints out a message that says “it has entered”.
I have recieved no results. I’ve even tried using the 3D triggers and nothing has happened…
Any ideas as to why?

Both have rigidbodies and aren’t kinematic (this would make my game not work) and I don’t know what you mean by the last part. Please elaborate because that is most probably the thing I have been overlooking that is causing this issue.

And yes, the function isn’t even being called…

This seriously has to be the weirdest rule I’ve encountered. It turns out the reason why it wasn’t being detected is because it was kinematic. Sorry everyone if this was a really nooby thing to miss but yeah, it was kinematic.
I got around the gravity thing by setting the RigidBody2D component’s “Gravity Scale” thing to 0 and everything is working fine and dandy now.

Thanks everyone for the input and I’ll edit the original post and what not to include this!