Issue: if condition doesn't work

Hi, i have a problem with this code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RemoveWall : MonoBehaviour
{
    private string wallDownTag = "Wall Down";
    private string wallLeftTag = "Wall Left";
    private string wallUpTag = "Wall Up";
    private string wallRightTag = "Wall Right";
    public Camera cam;
    public GameObject[] removed;

    void switchWall(float x, float y)
    {
        Debug.Log("Switch wall!");
        if (x == 90f){
            Debug.Log("X = " + x);
            Debug.Log("Do not remove wall!");           
            removeSelectable(wallDownTag, true);
            removeSelectable(wallLeftTag, true);
            removeSelectable(wallUpTag, true);
            removeSelectable(wallRightTag, true);
        } else if (x == 45f){
            Debug.Log("X = " + x);
            Debug.Log("Remove wall!");           
            wallPartsDown(y);
            wallPartsLeft(y);
            wallPartsUp(y);
            wallPartsRight(y);
        }
    }
    void wallPartsDown(float y){
        Debug.Log("Y = " + y);
        if (y == 0f || y == 45f || y == 315f)
        {
            removeSelectable(wallDownTag, false);
            if (y == 0f){               
                removeSelectable(wallLeftTag, true);
                removeSelectable(wallUpTag, true);
                removeSelectable(wallRightTag, true);
            } else if (y == 45f){
                removeSelectable(wallLeftTag, false);
                removeSelectable(wallUpTag, true);
                removeSelectable(wallRightTag, true);
            } else if (y == 315f){
                removeSelectable(wallLeftTag, true);
                removeSelectable(wallUpTag, true);
                removeSelectable(wallRightTag, false);
            }
        }
    }

    void wallPartsLeft(float y){
        Debug.Log("Y = " + y);
        if (y == 90f){
            removeSelectable(wallDownTag, true);
            removeSelectable(wallLeftTag, false);
            removeSelectable(wallUpTag, true);
            removeSelectable(wallRightTag, true);
        }
    }

    void wallPartsUp(float y){
        Debug.Log("Y = " + y);
        if (y == 135f || y == 180f || y == 225f){
            removeSelectable(wallUpTag, false);
            if (y == 135f){               
                removeSelectable(wallLeftTag, false);
                removeSelectable(wallDownTag, true);
                removeSelectable(wallRightTag, true);
            } else if (y == 180f){
                removeSelectable(wallLeftTag, true);
                removeSelectable(wallDownTag, true);
                removeSelectable(wallRightTag, true);
            } else if (y == 225f){
                removeSelectable(wallLeftTag, true);
                removeSelectable(wallDownTag, true);
                removeSelectable(wallRightTag, false);
            }
        }
    }

    void wallPartsRight(float y){
        Debug.Log("Y = " + y);
        if (y == 270f){
            removeSelectable(wallDownTag, true);
            removeSelectable(wallLeftTag, true);
            removeSelectable(wallUpTag, true);
            removeSelectable(wallRightTag, false);
        }
    }

    void removeSelectable(string tag, bool able){
        removed = GameObject.FindGameObjectsWithTag(tag);
        Debug.Log("Remove Selectable");
        foreach (GameObject remove in removed){   
            remove.GetComponent<Renderer>().enabled = able;
        }                  
    }
   
    void Update()
    {
        var camAngleX = cam.transform.eulerAngles.x;
        var camAngleY = cam.transform.eulerAngles.y;       

        switchWall(camAngleX, camAngleY);       
    }

}

This code should set the rendering of the walls, based on the angle of the camera rotation (0, 45, 90, etc …), to false. When the room is perpendicular to the xz plane (X = 90 °) all the walls must be visible and so far all right. The problem comes when X = 45 °, because the condition x == 45f is always detected as false and does not enter the if except at the first frame, in which it enters and “eliminates” the wall.

I entered the Debug.Logs to see what functions it goes into and “Switch wall!” each frame is printed.
Could someone kindly give me some explanation as to why it doesn’t enter the if?

Read this: The Floating-Point Guide - Comparison

TL;DR, don’t try to do direct floating point equality comparisons like this… it won’t work.

Thanks for the reply, I took a look at the link you sent and I found this in the official documentation thanks to your link and I put it as condition of the if but it still doesn’t work …

bool isEqual(float a, float b)
    {
        if (a >= b - Mathf.Epsilon && a <= b + Mathf.Epsilon)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

the weird thing, at least for me, it is that when X = 90 is fine, while in the other case it is not. I still don’t understand how to fix it.

If you are moving fast enough that you step past whatever Epsilon is, you could miss it.

Instead, check for “previous below and current beyond”:

  1. keep a previous value
  2. ask if the current value is beyond your limit while the previous is not (or vice-versa)
  3. at the end of the check, update previous to current

Computers are discrete machines, so if your speed is beyond the gap you’re looking for, you will always have issues like this. The key is to develop “edge sense” functions like the above.

The cam rotates through buttons, one to increase y by 45 degrees, the second to decrease y by 45, the third sets x to 45 and the last sets x to 90.

I solved… Instead of matching the two floats I check if the float is between two values. For example, instead of X == 90 I check if X is between 85 and 95. Thanks so much for the replies.