Detect a GameObject Passing Between Other Two GameObjects?

Guys is there a way to detect a gameObject is passing between(Not inside) other two gameobjects? Whenever it pass between other two objects, it sends me true. All of these three gameObjects are moving continuesly.Not static.

Assuming you’re talking about 2D space, you can calculate the sign of the determinant of vectors (AB,AC), where C is the object you’re testing to have crossed between the line AB. Pseudocode:

Side = sign( (B.x-A.x)*(C.y-A.y) - (B.y-A.y)*(C.x-A.x) )

This will be 0 if C is on the line from A-B, +1 on one side, and -1 on the other side.

Have you try to use collider on these objects? E.g., let say, you need to detect when A have passed through B and C.

  1. add some collider components on 3 objects, set trigger on to let them pass through each other.
  2. on A , insert a script, define some bools, say, isPassedB, isPassedC
  3. in OnTriggerEnter(), set isPassedB to true when trigger with B, vice versa
  4. set an if statement in Update() to do something when isPassedB and isPassedC both true.

Hope it helps. Tell me if you need the code.

A bit clunky and expensive but you could SweepTest between the two other objects

There is also SweepTestAll

How about a Raycast between your two objects ? You check the nature of the object hit by the ray : if it is your second object, return false, if another object passes, return true :slight_smile:

Unity’s documentation :