0
\$\begingroup\$

Let's say I have two different objects that can go through each other. Both have a circle collider (2d) that has the exact same size and there is a good chance that the two colliders will completely overlap each other. How can I trigger an event when that happens?

\$\endgroup\$
2
\$\begingroup\$

In that case you might just want to compare their positions:

if (transform.position == other.transform.position) {

Keep in mind that this checks for exact equality. Positions are floating point values and many games leave them to the physics engine, so they might differ by a miniscule amount which is invisible to the player but visible to the scripting engine. Actually, Vector3 overloads the == operator to return true when the distance between the vectors is smaller than 0.00001f, but even that might be too pedantic in some cases.

A better check might be to calculate the distance and trigger the condition when it is "close enough", in this example 1/100th of a unit:

if (Vector3.Distance(transform.position, other.transform.position) < 0.01f ) {
\$\endgroup\$
0
\$\begingroup\$

I'm not sure if there is a way to do that which covers all types of colliders, but for circle colliders you can do this:

For each circle collider, you have the object's position, then you can add the circle collider's offset to get he centre of the resulting circle collider. You can also get the circle collider's radius to compare how they overlap.

Let A be the position of the first circle collider (gameObject.position +circleCollider.offset) and B the same but for the second circle collider. Let Ar be the radius (circleCollider.radius) of the first circle collider and Br the radius of the second. Then:

// will be true if B is inside A's radius
bool aCollision = distance(A,B) <= Ar;

// will be true if A is inside B's radius
bool bCollision = distance(A,B) <= Br;

// this will be true if either A is inside B's radius, or B is inside A's radius
bool eitherCollision = aCollision || bCollision;

// this will be true if both A is inside B's radius, and B is inside A's
bool bothCollision = aCollision && bCollision;

This is a way to manually handle collision, so that you get the desired effect. Keep in mind though that the last line will only be true if A and B have the same radius, and the distance of each other is exactly that radius. Otherwise they can't both "include" each other at the same time.

Keep in mind this is pseudocode/C#. You would most likely need to replace distance with Vector2.Distance, or an equivalent in any other language.

I'm not sure what is the desired effect that you want, if you just want to make sure that both colliders simply overlap, without checking their radius or anything else, then @Phillipp's answer is way simpler.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.