-if two eulerAngles.z are the same do- isn't working when they're both 180

In my Grid RPG, the minimap draws a wall when the player is in front of one.
To not let it instantiate inifinite number of walls, it checks first if there’s already a wall on the position with the same rotation. This works perfect except when the rotation of the wall is 180, it still creates an infinite number of walls then. I can’t figure out why and I don’t know any other way to fix it.

I have to use EulerAngles and not RectTransform.rotation because I need to modify the rotation of the walls.

here’s a small piece of code that creates a wall after it checks if it isn’t created yet.

public void CreateWall(Vector3 pos)
{
    Vector2 wallPos = new Vector2(Mathf.Round(pos.x * 10 - 10), Mathf.Round(pos.z * 10 + 10));
    if(!CheckForWall(wallPos))
    {
        GameObject spawnedWall = (GameObject)Instantiate(wall);
        spawnedWall.transform.SetParent(wallsPanel.transform);
        spawnedWall.GetComponent<RectTransform>().anchoredPosition = wallPos;
        spawnedWall.GetComponent<RectTransform>().eulerAngles = playerSprite.GetComponent<RectTransform>().eulerAngles;
        spawnedWall.GetComponent<RectTransform>().localScale = new Vector3(1, 1, 1);
        walls.Add(spawnedWall);
    }
}
public bool CheckForWall (Vector2 pos)
{
    for (int i = 0; i < walls.Count; i++)
    {
        if (walls_.GetComponent<RectTransform>().anchoredPosition == pos && walls*.GetComponent<RectTransform>().localEulerAngles.z == playerSprite.GetComponent<RectTransform>().localEulerAngles.z)*_

{
return true;
}
}
return false;
}

There’s two problems here:

  • Internally, Unity uses quaternions to describe rotations. eulerAngles is just a conversion of the rotation, but it is not unique - the same quaternion rotation may be described by two different sets of eulerAngles, but which one you get is non-determinististic.
  • Each x,y,z component of the eulerAngle is itself a floating point number, and you cannot reliably compare equality of two floating point numbers due to inherent imprecision in the way they are expressed.

If you’re making a grid-based game, I recommend you create a simple int or enum variable that describes the rotation of each wall instance as 0,1,2,3 or N,S,E,W instead.