- Home /
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.
Right now, I am using line segment intersection algorithm. However, there are certain situations that line s. - line s. intersection is not doing well.
I will prepare a benchmark which compares 3 solution. 1. Line segment intersection currently I am using with the help of this(http://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/) 2. SweepTest 3.Raycast. I will inform you with results. $$anonymous$$eanwhile, all other ideas are welcome:)
Answer by tanoshimi · Jan 30, 2015 at 02:45 PM
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.
Remember that the line from B to A is not an infinite line. That is why C can be on +1 side of the line BA but still it may not be passed between them. However your solution provide me another view of perspective.Thanks.
I meant for you to test when the value changes from -1 to 1, or vice-versa. That would imply C had crossed the line AB.
Yes I take it, but still couldnt understand what if C becomes from -1 to +1 from right or left side of both A and B?
"what if C becomes from -1 to +1 from right or left side of both A and B?" I don't understand. If the sign of C changes, it's crossed the line.
Answer by Derek-Wong · Jan 29, 2015 at 03:31 PM
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.
add some collider components on 3 objects, set trigger on to let them pass through each other.
on A , insert a script, define some bools, say, isPassedB, isPassedC
in OnTriggerEnter(), set isPassedB to true when trigger with B, vice versa
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.
Thanks Derek for your answer. I already tried such kind of solution. But it didnt work in most cases.
Answer by meat5000 · Jan 29, 2015 at 03:37 PM
A bit clunky and expensive but you could SweepTest between the two other objects
http://docs.unity3d.com/ScriptReference/Rigidbody.SweepTest.html
There is also SweepTestAll
I dont know what SweepTest is. I will definetely search about it. Thanks.
Answer by Hellium · Jan 29, 2015 at 04:12 PM
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 :)
Unity's documentation :
Raycast : http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
RaycastHit : http://docs.unity3d.com/ScriptReference/RaycastHit.html (test on the
transform.name
for example)Raycasting tutorial : http://unity3d.com/learn/tutorials/modules/beginner/physics/raycasting
Ja ja, but not sure doing a raycasting in every Update frame is a good idea:/ If I find a optimized version of this solution, it will be the easiest and painless solution. Thanks man.
Another very simple solution is to check if the cross product between the two vectors AB and AC equals 0 or not. See this page about the cross product for explanation (middle of the page).
With Unity, the cross product between two vectors is simply computed with the Vector3.Cross
function : See doc.