Direction of another GameObject as Compass reading (N, NW, ...)?

Another question :slight_smile:

How can I determine the direction of GameObjectB relative to GameObjectA, as a compass reading (NW, N, NE, E, …)?

I have looked into this discussion

but it’s not exactly what I need (I think). In effect, I would have to go something like this (let’s say North is at the top of the viewport)

If B is “under” and to the “left” of A, then B is in South-Western direction relative to A.

The dot product is your friend here. Here is some pseudo code off the top of my head. It’s a simplification but should at least offer a starting point.

dirToB = Normalize(objB.position - objA.position)
northernness = Dot(dirToB, northDir)
easternness = Dot(dirToB, eastDir)

if(northernness > 0.7)
    compassDir = 'N'
else if(northernness < -0.7)
    compassDir = 'S'
else if(easternness > 0.7)
    compassDir = 'E'
else if(easternness < -0.7)
    compassDir = 'W'
    if(northernness > 0)
        compassDir += 'N'
        compassDir += 'S'
    if(easternness > 0)
        compassDir += 'E'
        compassDir += 'W'