Search Unity

Issue: if condition doesn't work

Discussion in 'Scripting' started by unity_229BtZKbQwn6Bg, Jan 15, 2021.

  1. unity_229BtZKbQwn6Bg

    unity_229BtZKbQwn6Bg

    Joined:
    Jan 15, 2021
    Posts:
    3
    Hi, i have a problem with this code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RemoveWall : MonoBehaviour
    6. {
    7.     private string wallDownTag = "Wall Down";
    8.     private string wallLeftTag = "Wall Left";
    9.     private string wallUpTag = "Wall Up";
    10.     private string wallRightTag = "Wall Right";
    11.     public Camera cam;
    12.     public GameObject[] removed;
    13.  
    14.     void switchWall(float x, float y)
    15.     {
    16.         Debug.Log("Switch wall!");
    17.         if (x == 90f){
    18.             Debug.Log("X = " + x);
    19.             Debug.Log("Do not remove wall!");          
    20.             removeSelectable(wallDownTag, true);
    21.             removeSelectable(wallLeftTag, true);
    22.             removeSelectable(wallUpTag, true);
    23.             removeSelectable(wallRightTag, true);
    24.         } else if (x == 45f){
    25.             Debug.Log("X = " + x);
    26.             Debug.Log("Remove wall!");          
    27.             wallPartsDown(y);
    28.             wallPartsLeft(y);
    29.             wallPartsUp(y);
    30.             wallPartsRight(y);
    31.         }
    32.     }
    33.     void wallPartsDown(float y){
    34.         Debug.Log("Y = " + y);
    35.         if (y == 0f || y == 45f || y == 315f)
    36.         {
    37.             removeSelectable(wallDownTag, false);
    38.             if (y == 0f){              
    39.                 removeSelectable(wallLeftTag, true);
    40.                 removeSelectable(wallUpTag, true);
    41.                 removeSelectable(wallRightTag, true);
    42.             } else if (y == 45f){
    43.                 removeSelectable(wallLeftTag, false);
    44.                 removeSelectable(wallUpTag, true);
    45.                 removeSelectable(wallRightTag, true);
    46.             } else if (y == 315f){
    47.                 removeSelectable(wallLeftTag, true);
    48.                 removeSelectable(wallUpTag, true);
    49.                 removeSelectable(wallRightTag, false);
    50.             }
    51.         }
    52.     }
    53.  
    54.     void wallPartsLeft(float y){
    55.         Debug.Log("Y = " + y);
    56.         if (y == 90f){
    57.             removeSelectable(wallDownTag, true);
    58.             removeSelectable(wallLeftTag, false);
    59.             removeSelectable(wallUpTag, true);
    60.             removeSelectable(wallRightTag, true);
    61.         }
    62.     }
    63.  
    64.     void wallPartsUp(float y){
    65.         Debug.Log("Y = " + y);
    66.         if (y == 135f || y == 180f || y == 225f){
    67.             removeSelectable(wallUpTag, false);
    68.             if (y == 135f){              
    69.                 removeSelectable(wallLeftTag, false);
    70.                 removeSelectable(wallDownTag, true);
    71.                 removeSelectable(wallRightTag, true);
    72.             } else if (y == 180f){
    73.                 removeSelectable(wallLeftTag, true);
    74.                 removeSelectable(wallDownTag, true);
    75.                 removeSelectable(wallRightTag, true);
    76.             } else if (y == 225f){
    77.                 removeSelectable(wallLeftTag, true);
    78.                 removeSelectable(wallDownTag, true);
    79.                 removeSelectable(wallRightTag, false);
    80.             }
    81.         }
    82.     }
    83.  
    84.     void wallPartsRight(float y){
    85.         Debug.Log("Y = " + y);
    86.         if (y == 270f){
    87.             removeSelectable(wallDownTag, true);
    88.             removeSelectable(wallLeftTag, true);
    89.             removeSelectable(wallUpTag, true);
    90.             removeSelectable(wallRightTag, false);
    91.         }
    92.     }
    93.  
    94.     void removeSelectable(string tag, bool able){
    95.         removed = GameObject.FindGameObjectsWithTag(tag);
    96.         Debug.Log("Remove Selectable");
    97.         foreach (GameObject remove in removed){  
    98.             remove.GetComponent<Renderer>().enabled = able;
    99.         }                  
    100.     }
    101.    
    102.     void Update()
    103.     {
    104.         var camAngleX = cam.transform.eulerAngles.x;
    105.         var camAngleY = cam.transform.eulerAngles.y;      
    106.  
    107.         switchWall(camAngleX, camAngleY);      
    108.     }
    109.  
    110. }
    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?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
  3. unity_229BtZKbQwn6Bg

    unity_229BtZKbQwn6Bg

    Joined:
    Jan 15, 2021
    Posts:
    3
    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 ......
    Code (CSharp):
    1.  
    2. bool isEqual(float a, float b)
    3.     {
    4.         if (a >= b - Mathf.Epsilon && a <= b + Mathf.Epsilon)
    5.         {
    6.             return true;
    7.         }
    8.         else
    9.         {
    10.             return false;
    11.         }
    12.     }
    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.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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.
     
  5. unity_229BtZKbQwn6Bg

    unity_229BtZKbQwn6Bg

    Joined:
    Jan 15, 2021
    Posts:
    3
    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.