- Home /
Question by
akajb84 · Dec 20, 2012 at 05:47 PM ·
locationbehindgame objects
Find what object is ahead of other
I have two game objects heading in relatively the same direction. How can I figure out which one is in front and which is behind?
For example, if you look at the image, B would be considered behind and A ahead.
Comment
Best Answer
Answer by akajb84 · Dec 20, 2012 at 06:49 PM
Oh, found my own answer: http://forum.unity3d.com/threads/53188-how-do-i-detect-if-an-object-is-in-front-of-another-object
To summarize those posts:
var heading : Vector3 = a.position - b.position;
var dProduct = Vector3.Dot(heading, a.forward);
if (dProduct > 0) {
// a is ahead
} else if (dProduct < 0) {
// b is ahead
} else {
// side by side
}