Doom-like angle based sprite changing

Hi guys!

So, i’m trying to make a first person game that used the same sprite mechanics as games like Doom, Duke Nukem and etc.

So far, i can identify the angle I’m at in relation to static objects, but not to rotating ones. I have some “enemies” that change rotation and start following me, but calculating the tangent angle (Mathf.Atan2) doesn’t take the enemy’s rotation in consideration.

Here’s the code i’m using so far, which works perfectly for objects that dont rotate:

    int GetAngleIndex()
    {
        var dir = cam.transform.position - transform.parent.forward;
        var enemyAngle = Mathf.Atan2(dir.z, dir.x) * Mathf.Rad2Deg;

        if (enemyAngle < 0.0f)
            enemyAngle += 360;

        Debug.Log("Angle from the player is: " + enemyAngle);

        if (enemyAngle >= 292.5f && enemyAngle < 337.5f)
            return 8;
        else if (enemyAngle >= 22.5f && enemyAngle < 67.5f)
            return 2;
        else if (enemyAngle >= 67.5f && enemyAngle < 112.5f)
            return 3;
        else if (enemyAngle >= 112.5f && enemyAngle < 157.5f)
            return 4;
        else if (enemyAngle >= 157.5f && enemyAngle < 202.5f)
            return 5;
        else if (enemyAngle >= 202.5f && enemyAngle < 247.5f)
            return 6;
        else if (enemyAngle >= 247.5f && enemyAngle < 292.5f)
            return 7;
        else if (enemyAngle >= 337.5f || enemyAngle < 22.5f)
            return 1;
        else return 0;
    }

I’ve searched for hours and I can’t find a solution to this :frowning:

float enemyAngle = Vector2.Angle(new Vector2(cam.transfrom.forward.x, cam.transfrom.forward.z), new Vector2(transform.parent.forward.x, transform.parent.forward.z));

Should be able to use that to get the angle between the camera’s forward and the enemy’s forward.